Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/454.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 无法访问backbone.js中的模型_Javascript_Backbone.js_Wcf Rest - Fatal编程技术网

Javascript 无法访问backbone.js中的模型

Javascript 无法访问backbone.js中的模型,javascript,backbone.js,wcf-rest,Javascript,Backbone.js,Wcf Rest,我从restful服务中得到了以下结果: 注意:响应是JSON格式的,是来自chrome的插件显示的。 如果您查看图像二[上面的一个],模型属性是Items,那么每个项都在Items下。我应该如何访问项目 我的问题是我无法从这个结果中访问或检索每个项目的数据。但我不能从服务器端更改任何内容。我用主干线来编写代码 window.Item = Backbone.Model.extend(); window.ItemCollection = Backbone.Collection.extend({

我从restful服务中得到了以下结果:

注意:响应是JSON格式的,是来自chrome的插件显示的。 如果您查看图像二[上面的一个],模型属性是Items,那么每个项都在Items下。我应该如何访问项目

我的问题是我无法从这个结果中访问或检索每个项目的数据。但我不能从服务器端更改任何内容。我用主干线来编写代码

window.Item = Backbone.Model.extend();

window.ItemCollection = Backbone.Collection.extend({
model: Item,
url: 'http://localhost/InterprisePOS/Product/loaditembycategory/Event Materials'
});

window.ItemListView = Backbone.View.extend({
tagName : 'ul',
initialize: function(){
    this.model.bind("reset",this.render,this);
},
render: function(eventName){
    _.each(this.model.models.Items, function(item){
        $(this.el).append(new ItemListItemView({model:item}).render.el);
    },this);
    return this;
}
 });

 window.ItemListItemView = Backbone.View.extend({
template : _.template($("#item-list").html()),

render: function(eventName){
    $(this.el).html(this.template(this.model.toJSON()));
    return this;
}
 });

 var AppRouter = Backbone.Router.extend({
routes:{
    "":"list"
},
list:function(){
    this.itemList = new ItemCollection();
    this.itemListView = new ItemListView({model:this.itemList});
    this.itemList.fetch();
    $("#itemContainer").html(this.itemListView.render().el);
}
 });

 var app = new AppRouter();
 Backbone.history.start();
使现代化 我能够纠正嵌套json对象的问题。现在,“模型属性”或“我的集合”中填充了单个项。但问题仍然是它不起作用,也不显示我的视图

这是我添加的代码:

parse: function(response) {
return response.Items;
}
使现代化
我终于回答了我的问题!霍瑞!不知何故,我忘了在ItemListview中启用render。而且$ItemContainer似乎不起作用,所以我使用了$ItemContainer,现在我正在显示模型中的详细信息

我相当确定主干网默认情况下对所有请求都使用JSON,并且不知道如何处理XML,您可能需要覆盖集合的sync方法,以便我们使用XML

以下内容有助于解决您的问题:


他们在同步解析操作中使用第三方库xml解析器,该解析器将模型转换为主干网可以本机使用的JSON。

确保响应作为JSON返回。主干在默认情况下对JSON数据起作用。

否。响应为JSON格式。不知何故,我在chrome上添加了一个插件,使图像显示的内容与您的相同,但我确信响应是JSON格式的。您不需要覆盖同步。您只需传递获取和保存选项即可处理XML。例如,对于fetch,您可以执行如下操作:this.model.fetch{contentType:application/json,dataType:xml,processData:false};就像一个符咒。它是JSON格式的。只是来自chrome的插件,它就是这样的: