나는 백본을 통해이를 수행하는 몇 가지 방법이 있다고 생각합니다. 그런 다음
var my_mash = new MashupModel({
dog: dogModel.toJSON(),
cat: catModel.toJSON(),
foxes: foxCollection.toJSON()
});
// do stuff if you need...
:
var MashupModel = Backbone.Model.extend({
});
그런 다음 당신은 당신이 평소처럼 어떤 모델을 전달 (또는 그 문제에 대한 모음) 할 수 있습니다 나는 매시업을 대표하는 모델로 시작 거라고 생각 응답이 올 때 당신이 원하는 일을 다시 정상 같은 :
my_mash.save({}, {
success: function(model, response) {
// do stuff here to put new data into the proper models/collections
},
error: function() { alert("I FAIL!"); }
});
잘하고 있다는 좋은 ... 그러나, 나는 르 MashupModel 개체로 대신의 요청에 따라 아래 위를 밀어 더 좋을 것 같아 벨. (백본가 아약스에 대한 준비 바인드합니다 얻기 위해 호출 이후)
var MashupModel = Backbone.Model.extend({
initialize: function(attrs) {
// can't remember the actual code, but something along the lines of:
_.each(attrs.keys, function(key) {
this.set(key, attrs.key.toJSON();
});
},
save: function(attrs, opts) {
var callback = opts.success;
opts.success = function(model, response) {
// do your conversion from json data to models/collections
callback(model, response);
};
// now call 'super'
// (ala: http://documentcloud.github.com/backbone/#Model-extend)
Backbone.Model.prototype.set.call(this, attrs, opts);
}
});
아니면 toJSON을 무시할 수 : 다시, 여러 가지 방법으로 지금
// class definition like above, no initilize...
...
toJSON: function() {
// again, this is pseudocode-y
var attrs = {};
_.each(this.attributes.keys, function() {
attrs.key = this.attributes.key.toJSON();
});
return attrs;
}
...
// save: would be the same as above, cept you'd be updating the models
// directly through this.get('dogs').whatever...
을, 당신은 다만 할 수 있습니다
var my_mash = new MashupModel({
dog: dogModel,
cat: catModel,
foxes: foxCollection
});
// do some stuff...
my_mash.save({}, {
success: function(model, response) {
// now only do stuff specific to this save action, like update some views...
},
error: function() { alert("I FAIL!"); }
매우 흥미 롭습니다. MashupModel과 같은 추상화 된 백본 모델을 만들려고했지만 그 전에 커뮤니티를 조사했습니다. 실험을 어떻게 진행했는지에 대해보고하고 아마 그 답을 받아 들일 것이라고 생각합니다. 다시 한 번 감사드립니다! – papdel