【问题标题】:Backbone collection, get specific attribute骨干集合,获取特定属性
【发布时间】:2015-12-29 20:07:52
【问题描述】:

我在我的 Backbone 集合上设置了一个属性 currentPage,但是我似乎无法专门记录它。

    onRender: function () {
        App.log('this.collection.currentPage onRender')
        App.log(this.collection)
        App.log(this.collection.accountID)
        App.log(this.collection.currentPage)
        App.log(this.collection.get('currentPage'))

    }

我执行获取然后显示,并在 onRender 中生成以下控制台日志

其中this.collection.currentPagethis.collection.get('currentPage')undefined,尽管我可以在this.collection 的日志输出中将currentPage 视为已定义的(“2”)属性。 this.collection.accountID 正在按照我期望的 currentPage 工作。

我做错了什么?

编辑:我在.fetchsuccess 上设置属性,因为数据在响应标头中返回。像这样的,

getAccountSegments: function (accountID) {
    var accountSegmentsCollection = new Entities.Collections.AccountSegmentCollection({
        accountID: accountID
    });

    accountSegmentsCollection.fetch({
        success: function (collection, response, options) {
            collection.currentPage = options.xhr.getResponseHeader('X-Current-Page');
        }
    });

    return accountSegmentsCollection;
},

下面有人说我不允许在集合上设置属性(除了通过构造函数,但那是在获取之前)。那么我还能如何将这个响应头数据放到我的视图中呢?

【问题讨论】:

  • 您何时/何地设置属性..?也许你在渲染后设置它。我们怎么知道..?分享相关代码。
  • @TJ 查看我的编辑,我在.fetch 之后设置它。奇怪的是,我看到 currentPage 在 onRender 中正确记录了集合,但当我直接记录 currentPage 时却没有。
  • 仍然不清楚onRenderfetch 的调用顺序。您何时/如何致电getAccountSegmentsonRender..?你确定渲染发生在 fecth.. 之后吗?

标签: javascript backbone.js collections console.log


【解决方案1】:

主干 js 中的集合不允许您设置属性。 将元信息存储在骨干集合中的最佳方式是通过纯 javascript 对象。

例如:

var myCollection = Backbone.Collection.extend({
    model: myModel,

    metaInfo: {
      currentPage: "2"
    }
});

然后你可以使用this.collection.metaInfo.currentPage在你的视图中获取当前页面

【讨论】:

  • 这里的问题是metaInfo 将附加到集合原型并因此由所有实例共享,我认为您希望在集合的initialize 方法中改为this.metaInfo = { currentPage: '2' }
  • 但是元信息不是应该被所有实例访问吗?
  • 问题仍然存在,因为我试图在.fetchsuccess 上设置currentPage。服务器返回带有X-Current-Page 标头的分页数据。成功时我会做collection.metaInfo.currentPage = options.xhr.getResponseHeader('X-Current-Page');,但它设置不正确。当我进入我的视图时,它仍然是未定义的(或者,根据您的建议,在集合构造函数中设置为默认值)。我还能如何将此标头数据放入我的视图中?
  • 我正在尝试创建“X of Y Items”标签。所以我同样有一个collection.metaInfo.totalCount = options.xhr.getResponseHeader('X-Total-Count'); 标头要传递。
  • 是的,但它可能不应该出现在原型上:jsfiddle.net/ambiguous/yxv37qhz
【解决方案2】:

get 在这种情况下有点令人困惑。在 Backbone 中,模型有属性,但集合没有,这意味着集合上的 get 与模型上的 get 不同。

get 在模型上检索一个属性,而在一个集合上它将返回一个模型(通过 id)。您可以扩展您的集合以具有属性获取器和设置器:

var pageCollection = Backbone.Collection.extend({
    getAttribute: function(attr) {
        return this.attributes?this.attributes[attr]:null;
    },
    setAttribute: function(attr,val){
        this.attributes = this.attributes || {};
        this.attributes[attr] = val;
    }
});

然后你可以设置你的 currentPage 属性:

pageCollection.setAttribute('currentPage', 'myPage');

然后检索它:

App.log(pageCollection.getAttribute('currentPage');

这应该为您提供您所追求的功能。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-13
    • 1970-01-01
    • 2013-09-28
    • 1970-01-01
    • 1970-01-01
    • 2014-01-04
    • 1970-01-01
    • 2012-12-02
    相关资源
    最近更新 更多