2013-06-17 2 views
0

나는 backbone.js에서 새로운입니다. 나는 백본 결과에서 목록을 가져 오려고 노력하고 있습니다.Backbone.js에서 결과보기

모델 : - - : 여기 내 코드는

public class Company12 
    { 
    public List<Company1> GetCompanies { get; set; } 
    public int Page { get; set; } 
    } 

그리고 내의 .js는

$(function() { 


window.Company = Backbone.Model.extend({ 
    defaults: function() { 
     return { 
      order: Companies.nextOrder() 
     }; 
    } 
}); 

window.CompanyList = Backbone.Collection.extend({ 

    // Reference to this collection's model. 
    model: Company, 
    url: function() { 
     var s = "api/Companies" 
     return s; 
    }, 
    nextOrder: function() { 
     if (!this.length) return 1; 
     return this.last().get('order') + 1; 
    } 


}); 

// Create our global collection of **Todos**. 
window.Companies = new CompanyList; 


// Todo Item View 
// -------------- 

// The DOM element for a todo item... 
window.CompaniesView = Backbone.View.extend({ 

    //... is a list tag. 
    tagName: "li", 

    // Cache the template function for a single item. 
    template: _.template($('#item-template').html()), 

    // The DOM events specific to an item. 
    events: { 


    }, 

    // The TodoView listens for changes to its model, re-rendering. 
    initialize: function() { 
     this.model.bind('change', this.render, this); 
     this.model.bind('destroy', this.remove, this); 
    }, 

    // Re-render the contents of the todo item. 
    render: function() { 
     $(this.el).html(this.template(this.model.toJSON())); 
     this.setText(); 
     return this; 
    }, 

    // To avoid XSS (not that it would be harmful in this particular app), 
    // we use `jQuery.text` to set the contents of the todo item. 
    setText: function() { 
     var text = this.model.get('text'); 

     this.$('.name-text').text(text); 
    } 
}); 

// The Application 
// --------------- 

// Our overall **AppView** is the top-level piece of UI. 
window.AppView = Backbone.View.extend({ 
    // Instead of generating a new element, bind to the existing skeleton of 
    // the App already present in the HTML. 
    el: $("#companiesRoot"), 
    // At initialization we bind to the relevant events on the `Todos` 
    // collection, when items are added or changed. Kick things off by 
    // loading any preexisting todos that might be saved in *localStorage*. 
    initialize: function() { 
     this.input = this.$("#new-todo"); 
     Companies.bind('add', this.addOne, this); 
     Companies.bind('reset', this.addAll, this); 
     Companies.bind('all', this.render, this); 
    }, 

    // Add a single todo item to the list by creating a view for it, and 
    // appending its element to the `<ul>`. 
    addOne: function (company) { 
     var view = new CompaniesView({ model: company }); 
     this.$("#company-list").append(view.render().el); 
    }, 

    // Add all items in the **Todos** collection at once. 
    addAll: function() { 
     Companies.each(function (p, s) { 
      alert(JSON.stringify(p)); 

     }); 
     Companies.each(this.addOne); 
    } 

}); 
// Finally, we kick things off by creating the **App**. 
window.App = new AppView; 

파일}); 내가 무엇입니까 모두 추가 기능에서

: -

{"order":1,"GetCompanies":[{"id":21,"text":"Testing Company","done":false,"order":3},{"id":10,"text":"WebTech Solution","done":false,"order":3},{"id":5,"text":"Software Solution","done":false,"order":3}],"Page":0} 

내가 모든 GetCompanies를 가져올 수있는 방법이 결과. 미리 감사드립니다.

답변

1

컬렉션을 가져올 때 목록이 포함 된 JSON을 반환하는 동안 컬렉션을 가져와야합니다. 따라서 여기에 2 가지 솔루션이 있습니다.

  1. 응답 서버 측 수정 : GetCompanies 부분 만 보내십시오.
  2. 응답 클라이언트 쪽 (Collection#parse)을 수정하십시오.

    parse: function(response) { 
        return response.GetCompanies; 
    } 
    

    백본 당신이 반환 무엇 내부의 값을 반복, 그리고 당신의 모델로 정의하기 때문에 (사람들과 Company을 생성합니다 :

그래서 당신은 당신의 콜렉션 내부에 그런 일이있을 것이다 귀하의 컬렉션).

+0

안녕하세요. @Loamhoof –