2016-01-24 85 views
0

放弃,不知道为什么骨干没有获取我的JSON。 我的检查员说:“未捕获的ReferenceError:业务未定义” 任何人都可以帮助我?骨干没有得到我的JSON

这里是我的代码:

//------------- 
    // Model: 
    //------------- 

    var DirectoryItem = Backbone.Model.extend();  


    //------------- 
    // Collection: 
    //------------- 

    var Directory = Backbone.Collection.extend({  
    model: DirectoryItem, 
    url: 'JSON/directory.json', 

    parse: function (data) { 
     return data.Businesses 
    } 

    }); 

    //------------- 
    // List View: 
    //------------- 

    var DirectoryListView = Backbone.View.extend({ 

    el: $("#directoryView"), 

    events: { 
     "click li": "itemClicked" 
    }, 

    initialize: function() { 
     this.collection = new Directory(); 

     this.collection.fetch(); 
     this.render();     
     this.bind('change', this.render, this); 
    }, 

    render: function() { 
     var business = new Directory(); 

     var that = this; 

     business.fetch({ 
     success: function (Directory) { 
      var template = _.template($('#item-list-template').html(), {Directory: Directory.models}); 
      that.$el.append(template); 
     } 
     }); 
     that.$el.toggleClass('view'); 

     return that; 
    }, 

    itemClicked: function(e){ 
     //we get the id of the clicked item throught his data property from the html 
     var id = $(e.currentTarget).data('id'); 
     //we obtain the right model from the connection with the id 
     var name = this.collection.get(id); 
     //we load the view of the selected model 
     var directoryItemView = new DirectoryItemView({ model: name }); 
    } 

    }); 

这里是我的模板:

<script type="text/template" id="item-list-template"> 
    <li><%= Business %></li> 
</script> 

这里是我的JSON的例子:

{ 

    "Businesses":[ 
    { 
     "Business" : "Busines nº1", 
     "Field" : "Marketing - web dev agency", 
     "Contact Info" : "[email protected]", 
     "Link" : "http://www.web.com/contact-us", 
     "Where?" : "Downtown", 
     "Round:" : 0, 
     "follow up" : "", 
     "Comments" : "" 
    }, 
    { 
     "Business" : "Busines nº2", 
     "Field" : "University", 
     "Contact Info" : "", 
     "Link" : "https://www.web.edu", 
     "Where?" : "", 
     "Round:" : 0, 
     "follow up" : "", 
     "Comments" : "" 
    },... 

非常感谢!

+1

http://stackoverflow.com/a/25881231/1071630 – nikoshr

+2

所以你使用遍历并访问Business Underscore 1.7.0+作为@nikoshr的东西,或者你是否在使用旧的Underscore,并且当它期望看到“{Business:...}”时,给出你的模板“{Directory:...}”?都? –

+0

是的,我使用了一个更新的下划线,现在我不能尝试@nikoshr建议的内容,但可能今晚我可以,谢谢! – Trifit

回答

2

您传递给下划线的数据({Directory: Directory.models})没有Business属性,因此是错误。

什么你传球的模型数组,你应该从每个模型

<% _.each(Directory, function(model) { %> 
    <li> <%= model.get("Business") %> < /li> 
<% }); %> 
+0

我从标记这在我的HTML内联?要么我仍然做错了什么,或者我不知道发生了什么,因为我仍然收到错误,现在说:“未捕获的ReferenceError:目录未定义”。我忘了提及我使用Mongoose作为服务器来避免CORS。 – Trifit

+1

@Trifit由于您使用的是最新版本的下划线,因此您需要通过'_.template()'将数据传递给模板函数返回,而不是作为它的第二个参数。如果这不起作用,可以使用代码编辑器创建[mcve]。有一个内置的 –

+0

真的很抱歉,我发誓我尝试了@T J提及并没有工作,但昨天,我重构了@TJ解决方案之后的功能,现在可以工作,无论如何感谢你们所有人 – Trifit