我对backbone.js有疑问。我正在为现有的api创建一个前端,因为我无法访问。问题是当我尝试将新模型添加到集合时,我可以在我的firebug中看到,每次主干尝试创建模型时,它都会将属性名称附加到URL。
示例:
任何人都知道如何避免这种行为?
Greetings Kern
我的观点:
window.databaseView = Backbone.View.extend({
el: '#content',
template: new EJS({url: 'js/templates/databaseView.ejs'}),
initialize: function() {
var self = this;
this.collection.fetch({
success: function() {
console.log(self.collection);
var test = self.collection.get("_system");
console.log(test);
self.collection.get("_system").destroy();
self.collection.create({name: "test"});
}
});
},
render: function(){
$(this.el).html(this.template.render({}));
return this;
}
});
型号:
window.Database = Backbone.Model.extend({
initialize: function () {
'use strict';
},
idAttribute: "name",
defaults: {
}
});
收藏:
window.ArangoDatabase = Backbone.Collection.extend({
model: window.Database,
url: function() {
return '../../_api/database/';
},
parse: function(response) {
return _.map(response.result, function(v) {
return {name:v};
});
},
initialize: function() {
this.fetch();
},
getDatabases: function() {
this.fetch();
return this.models;
},
dropDatabase: function() {
},
createDatabse: function() {
}
});
答案 0 :(得分:2)
默认情况下,Backbone以这种方式创建模型URL:{collection url}/{model id}
。
它认为集合URL是RESTful方式的基本URL。
在这里,您只想将Model url
属性设置为您要调用的URL。这将覆盖默认行为。 http://backbonejs.org/#Model-url