2016-01-24 92 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