2012-09-28 4 views
0

그래서 레일스 -API 젬을 사용하여 레일 API를 만들었습니다. 그리고 백본 코드를 기본 public/index.html에 넣습니다. json 링크를 시도하고 json을 반환합니다.이 모델은 기본 레일 필드와 함께 'content'라는 단일 필드가있는 '항목'이라는 모델입니다. 하지만 백본 코드가 작동하지 않습니다.이 문제가 발생할 수 있습니까? 난 그냥 반환 된 json에서 레코드 목록을 표시하려고합니다.rails 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

    에시 추가 변경 사항을 추가 –

    답변

    0

    '항목'이 '결과'노드를 반환하는지 확인 할 수 있습니까? 아마 Rails에서 ActiveRecord :: Base.include_root_in_json = false를 잊어 버렸습니까? 당신의 AppRouter 코드에서

    0

    , 당신은이 라인을 가지고 :이처럼 정의해야

    그러나
    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) { 
    

    나는 그것이 전부인지는 확실하지 않지만, 그것은 나에게 즉시 명백한 유일한 것입니다.

    편집 :이 링크 = https://github.com/documentcloud/backbone을보고 이것을 시도 this.model 참조