我的骨干网有问题.js。我正在为现有 API 创建一个前端,对我来说无法访问。问题是,当我尝试将新模型添加到集合时,我可以在我的Firebug中看到,每次主干尝试创建模型时,它都会将属性名称附加到url中。
例:
- 默认网址 =/API/数据库
- 当我执行 GET =/api/数据库时
- 当我使用对象 {"名称"执行 GET/POST 时: "测试"} =/api/database/test 是结果
有人知道如何避免这种行为吗?
问候克恩
我的观点:
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() {
}
});
默认情况下,Backbone 通过以下方式创建模型 URL:{collection url}/{model id}
.
它将集合 URL 视为 RESTful 方式的基本 URL。
在这里,您只想将模型url
属性设置为要调用的 URL。这将覆盖默认行为。http://backbonejs.org/#Model-url