3
我正在使用Backbone js,我試圖用數據使用fetch填充模型。問題是提取似乎正在工作,但我的模型沒有填充數據。Backbone js和使用fetch填充數據的模型
的代碼片段:
Backbone.emulateHTTP = true;
Backbone.emulateJSON = true;
ComponentsModel = Backbone.Model.extend({
initialize : function() {
},
defaults : {
component_id : null
},
urlRoot : "/components/ajax_component",
});
ComponentsView = Backbone.View.extend({
el : $('body'),
events : {
'change #component-selector' : 'changeComponent',
},
initialize : function() {
_.bindAll(this, 'render', 'changeComponent');
this.render();
},
changeComponent : function(e) {
var clickedEl = $(e.currentTarget);
var value = clickedEl.attr("value");
var component = new ComponentsModel({id :value, component_id :value });
component.fetch();
component.toJSON();
alert(component.get('component_name'));
},
render : function() {
},
});
,並從服務器的JSON作爲回報看起來是這樣的:
{"component_id":"1","component_name":"Test Component 1","component_description":"A simple test component","component_required":"","user_id":"1","component_approved":"0","component_price":"0","component_overview":"'"}
警報始終是不確定的。我錯過了什麼嗎?