2012-09-28 35 views
0

所以我創建了一個使用rails-api gem的rails api。我把我的骨幹代碼放在默認的public/index.html中。我嘗試了json鏈接並返回json,它只是一個名爲'entry'的模型,其中包含一個名爲'content'的字段以及一些默認的rails字段。但骨幹代碼不起作用,你能發現這個問題嗎?我只是試圖從返回的json中顯示記錄列表。簡單的骨幹示例與導軌api

 
// Models 
window.Entry = Backbone.Model.extend(); 
window.EntryCollection = Backbone.Collection.extend({ 
    model:Entry, 
    url:"http://localhost:3000/entries" 
}); 

// Views 
window.EntryListView = Backbone.View.extend({ 
    tagName:'ul', 
    initialize:function() { 
     this.model.bind("reset", this.render, this); 
    }, 
    render:function (eventName) { 
     _.each(this.model.models, function (entry) { 
      $(this.el).append("
  • " + entry.get("content") + "
  • "); }, this); return this; } }); // Router var AppRouter = Backbone.Router.extend({ routes:{ "":"list" }, list:function() { this.entryList = new EntryCollection(); this.entryListView = new EntryListView({model:this.entryList}); this.entryList.fetch(); $('#entries').html(this.entryListView.render().el); } }); var app = new AppRouter(); Backbone.history.start();
    +0

    看到這個鏈接= https://github.com/documentcloud/backbone和嘗試這個 –

    回答

    0

    你可以檢查,如果/條目返回'結果'節點? 也許你忘了在Rails中ActiveRecord :: Base.include_root_in_json = false?

    0

    在你AppRouter的代碼,你有這樣一行:

    this.entryListView = new EntryListView({model:this.entryList}); 
    

    然而,entryList是一個集合,不是模特,所以你必須將它定義是這樣的:同樣

    this.entryListView = new EntryListView({collection:this.entryList}); 
    

    ,在EntryListView代碼,您有:

    this.model.bind("reset", this.render, this); 
    
    ... 
    
    _.each(this.model.models, function (entry) { 
    

    應該變成:

    this.collection.bind("reset", this.render, this); 
    
    ... 
    
    _.each(this.collection.models, function (entry) { 
    

    我不確定這是否是一切,但它是唯一立即對我顯而易見的事情。

    編輯:增加了用戶互動,增加變化this.model參考