【问题标题】:Get List from Backbone.js result从 Backbone.js 结果中获取列表
【发布时间】:2013-06-17 11:34:06
【问题描述】:

我是主干.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;

});

在 Addall 函数中,我得到了:-

{"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。 请帮助提前谢谢。

【问题讨论】:

    标签: jquery backbone.js


    【解决方案1】:

    当您获取一个集合时,您应该返回一个列表,同时您返回一个包含列表的 JSON。所以你有两个解决方案:

    1. 修改响应服务器端:仅发送GetCompanies 部分。
    2. 修改响应客户端(使用Collection#parse)。

    所以你的收藏中会有类似的东西:

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

    Backbone 将遍历您返回的值,并使用这些值创建 Company(因为您将其定义为集合的模型)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-15
      • 1970-01-01
      • 1970-01-01
      • 2018-02-24
      • 2013-05-06
      • 1970-01-01
      相关资源
      最近更新 更多