Knockout.js observableArray 与 Backbone.js Collection - 有什么区别?
在 Knockout.js 中,我创建一个 observableArray 将模型推入:
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);
}
在 Backbone.js 中,我将创建一个集合来存储我的模型:
var Book = Backbone.Model.extend();
var Books = new Backbone.Collection([
{name: "Abe Lincoln - Vampire Hunter"}
{name: "Pride and Prejudice and Zombies"}
]);
这两种结构彼此有何不同?
幕后究竟发生了什么使这些数据结构与标准 Javascript 数组不同?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个很难完全回答的问题,但这是我的看法:)。
Backbone.js Collection:
Knockout.js observableArray:
.indexOf()
)destroy
& Rails 开发人员的destroyAll
方法将对象上的_destroy
属性设置为true
- 这将通知 Rail 的 ActiveRecord 应删除哪些对象。Backbone.Collection
与 Backbone.js 框架配合使用,而observableArray
仅与 Knockout.js 配合使用。单独比较它们是没有实际意义的,因为它们是框架的一部分,如果您的应用程序是基于 Backbone 构建的,则不能使用 observableArray,反之亦然。如果您想知道幕后到底发生了什么,那么这里是源代码:
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 totrue
- this will inform Rail's ActiveRecord which objects should be deleted.Backbone.Collection
is working with the Backbone.js framework andobservableArray
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 useobservableArray
and vice versa.If you want to know what exactly is going on behind the scenes then here is the source code: