Jorin
Jorin

Reputation: 1662

Best way to save changes in MVVM/Knockoutjs Web App?

I've been playing with KnockoutJS and absolutely love how much it simplifies the design from every angle by keeping stuff from falling through the cracks. My question is what is the recommended "best practice" for saving the data back to the server? My understanding is that in a connected MVVM, the first "M" is the data layer and so the dependency tracking and notifications in the ViewModel trigger saves directly back to the data layer. In a JavaScript app, we are disconnected and selectively save back to the server using AJAX.

The app I'm currently using it in is MVC3 and I absolutely get how to write a "Save" action on my controller, plop a "Save" button somewhere on my page, post the whole ViewModel to that Save action and then persist that to the database. But what about when you make a quick edit and then save it again? Or what if a save button doesn't fit the flow of the design? Instead, you want to post to the action every time a change is made on the form with no save button at all? The ideas that I've bounced around are:

I'm hopeful there are some good ideas out there that I haven't thought of. To be able to declaratively bind everything AND still save efficiently would be awesome.

Upvotes: 9

Views: 4126

Answers (3)

dylanized
dylanized

Reputation: 3855

You might check out the Mapping plugin for Knockout, it lets you have load up Knockout from a JSON array. If it wasn't too big, you save that array down to server on a timer (or after a change). Hope this helps, sorry if you already knew this.

http://knockoutjs.com/documentation/plugins-mapping.html

http://knockoutjs.com/documentation/json-data.html

Upvotes: 0

David Wick
David Wick

Reputation: 7095

The only other thing i could think of is subscribing. When i first started reading your post i was thinking flags w/grep though.

Edit: Better yet, ko.utils.compareArrays looks promising.

Here's a working example..

The only thing left to do is detect changes to values of the 'retained' values. You're well on your way though.

Upvotes: 0

Paulczy
Paulczy

Reputation: 641

I just got back from Mix11 where I attended this session about Knockout.js. It might be worth your while to watch Steve Sanderson crank out a full CRUD demo.

Upvotes: 5

Related Questions