Building an efficient subscription for knockout computed observables

Subscribing to a ko.computed isn’t the same thing as subscribing to a ko.observable. Ryan Niemeyer has written on the topic and I’ll admit, it took me a while to wrap my head around it. I’ll go over the problem and how we bypass it, but the end result is really handy and reusable: a new ‘efficientlySubscribe’ method that is a part of all ko.computed’s.

[Continue Reading...]

0 Responses to Building an efficient subscription for knockout computed observables

Extending Ryan Niemeyer’s protectedComputed

Ryan Niemeyer has a great article on the protectedComputed that he came up with, but I needed a little more functionality out of it. I ended up adding a few things and wanted to get it online for public consumption if anyone found the new functionality useful, so here it is!

[Continue Reading...]

0 Responses to Extending Ryan Niemeyer’s protectedComputed

There’s no reason to NOT use css sprites anymore

Sprites have always been great for web development but it was a significant undertaking to actually implement them – Not anymore though!

Most developers I know didn’t bother implementing them because of the pain associated with maintaining your sprite and the css classes. Sure you could use an online generator but that was a pain because it wasn’t a super simple process to add / remove images. You’d have to go to a site like css-sprit.es or csssprites.com and upload all your images and then update the project with the updated sprite and css. Bleh, that’s a huge pain. Don’t get me wrong, those sites were extremely useful for those that had to use them, but all this stuff should be done for us nowadays.

I love the idea of sprites but this was too much work, thankfully Microsoft came up with an awesome NuGet package that will probably be a part of MVC4!

[Continue Reading...]

2 Responses to There’s no reason to NOT use css sprites anymore

Changing an observableArray in 1 notification

I was just reading Ryan Niemeyer’s 2nd post about performance in knockout.js and I saw arrayPushAll. We ended up needing to use it for reasons other than performance, but performance does benefit greatly from its use.

We just had a problem (4/6/2012) where we were adding an array of items, one at a time in a loop, to an observable array. We wanted that notification of the array changing to trigger an ajax request, but we couldn’t because we were getting {n} notifications and we only wanted one request.

Initially I had to wire up the event in an odd way and that approach didn’t feel right since it seemed that we should be able to use the arrays changed notification. I should have thought to look up if ko had a slice or an equivalent, but I didn’t at the time. Now we just use arrayPushAll and this gives us a single notification because its a single change to the array. This is a much better solution and the added performance is great too!

0 Responses to Changing an observableArray in 1 notification

Knockout.js: dependent vs computed observables

I’ve been going over alot of knockout work lately and I’m new to 2.0. I’d seen material on dependent observables and then some stuff on computed observables and thought they were different things! I’d been meaning to google what their differences were when I ran across this on Knockoutjs:

What happened to dependent observables?

Prior to Knockout 2.0, computed observables were known as dependent observables. With version 2.0, we decided to rename ko.dependentObservable to ko.computed because it is easier to explain, say, and type. But don’t worry: this won’t break any existing code. At runtime, ko.dependentObservable refers to the same function instance as ko.computed — the two are equivalent.

That makes a lot of sense now! Yay one less thing to have to know. You should check out the API, its pretty useful. I’m going to try and write a small blurb about using a computed observable in a separate viewModel to read/write data from cookies.

2 Responses to Knockout.js: dependent vs computed observables