In Knockout.js I create an observableArray to push models into:
function Room(data) {
this.name = ko.observable(data.name);
}
function RoomViewModel() {
var self = this;
self.rooms = ko.observableArray([]);
self.newRoomText = ko.observable();
self.addRoom = function() {
self.rooms.push(new Room({ name: this.newRoomText() }));
self.newRoomText("");
$("#modal").dialog("close");
}.bind(self);
}
In Backbone.js I would create a collection to store my models:
var Book = Backbone.Model.extend();
var Books = new Backbone.Collection([
{name: "Abe Lincoln - Vampire Hunter"}
{name: "Pride and Prejudice and Zombies"}
]);
Just how different are these 2 structures from each other?
What exactly is going on behind the scenes to make these data structures different from a standard Javascript Array?
This is a difficult question to fully answer but here is my take on it :).
Backbone.js Collection:
Knockout.js observableArray:
.indexOf()
)destroy
& destroyAll
methods for Rails developer that will set _destroy
property on objects to true
- this will inform Rail's ActiveRecord which objects should be deleted.Backbone.Collection
is working with the Backbone.js framework and observableArray
only with Knockout.js. There is no real in comparing them with each other in a isolation because they are part of a framework and if your application is built on Backbone you cannot use observableArray
and vice versa.
If you want to know what exactly is going on behind the scenes then here is the source code:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With