Đây là giải pháp hoàn chỉnh của tôi cho câu hỏi này: jsFiddle Complete example
@spotmat đưa cho tôi định hướng đúng, nhưng tôi cần phải thêm chức năng bổ sung. Dưới đây là một vài vật phẩm cần thiết để được giải quyết:
- Collection contructor cần để xử lý dữ liệu pased để nó
- Khi một mô hình sở hữu IsLocked được cập nhật các bộ sưu tập cần phải được tái xác nhận
tôi 'không chắc chắn nếu thêm thuộc tính ràng buộc vào một bộ sưu tập là một ý tưởng tuyệt vời, nhưng là một cái gì đó thú vị để tìm ra, và tôi đã học được rất nhiều.
var log = {};//this is for debugging
_.extend(log, Backbone.Events);
var Car = Backbone.Model.extend({});
var Cars = Backbone.Collection.extend({
model: Car,
url: "scripts/data/Cars.json",
initialize: function() {
var _this = this;
this.isValid = false; //user should bind to "change:isValid"
this._isPending = false; //This is so that
this.bind("add", this.modelAdded, this);
this.bind("reset", this.modelsAdded, this);
if (this.length > 0) {
//Model passed in though the constructor will not be binded
//so call modelsAdded
this.modelsAdded(this);
}
},
modelsAdded: function (collection) {
this._isPending = true
log.trigger("log", ["modelsAdded: " + collection.length]);
collection.each(this.modelAdded, this); //Do nut remove "this" param. It need when modelAdded gets called
this._isPending = false
this._validate();
},
modelAdded: function (model) {
log.trigger("log", ["modelAdded: " + model.get("Model") + "; IsLocked: " + model.get("IsLocked")]);
//!!!for each model added to the colleciton bind
//its IsLocked property to the collection modelIsLockedChange function
model.bind("change:IsLocked", this.modelIsLockedChanged, this);
if (this._isPending == false) {
this._validate();
}
},
modelIsLockedChanged: function (model) {
log.trigger("log", ["modelIsLockedChanged:" + model.get("Model") + "; IsLocked: " + model.get("IsLocked")]);
this._validate();
},
_validate: function() {
var isValid = true;
this.each(function (model) {
if (model.get("IsLocked") == false) {
isValid = false
}
});
if (this.isValid != isValid) {
this.isValid = isValid
cars.trigger("change:isValid", [this.isValid])
}
},
});
Quan điểm này là dành cho mục đích gỡ lỗi chỉ
var Logger = Backbone.View.extend({
el: $("#output"),
initialize: function(){
log.bind("log", this.logMessage, this)
},
render: function(){
return $(this.el).html("");
},
logMessage: function(message){
$(this.el).find("ul").append("<li>" + message[0] + "</li>");
}
})
Các mã sau đây được sử dụng để kiểm tra bộ sưu tập
var logger = new Logger();
log.bind("log", function(message){
console.log(message[0]);
});
var carsData = [
{ "Model": "Chevy Camero", "Year": 1982, "IsLocked": true },
{ "Model": "Ford F-150", "Year": 2011, "IsLocked": false }
]
var cars = new Cars(carsData);
cars.bind("change:isValid", function(isValid){
log.trigger("log", ["change:isValid: " + isValid]);
});
cars.add({ "Model": "Toyota Tacoma", "Year": 2006, "IsLocked": true });
cars.at(1).set({ "IsLocked": true });
Cảm ơn câu trả lời. Tôi tin rằng bạn đã chỉ cho tôi theo đúng hướng. Đánh giá cao thông tin đó làValid nên được tạo trong khởi tạo. Khi tôi kết thúc với giải pháp làm việc của mình, tôi sẽ đăng nó. –