javascript 如何在 Backbone 模型中正确实现解析以包含集合?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9491902/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 06:51:08  来源:igfitidea点击:

How can I correctly implement parse in a Backbone model to include collections?

javascriptbackbone.js

提问by rybosome

I have a backbone model called Member- this contains membership data such as first name, last name, email, phone, etc. It also includes multi-valued fields that I need to keep as collections - these are Degreesand Affiliations.

我有一个名为的主干模型Member- 它包含成员资格数据,例如名字、姓氏、电子邮件、电话等。它还包括我需要作为集合保留的多值字段 - 这些是DegreesAffiliations

The issue I am running into when calling the fetch()method in my model is that there's an impedance mismatch of sorts between a basic array, and the collection objects I have in my model. Since parseby definition is supposed to return a hash to be used in setrather than actually setting values, it is impossible for me to set my collections this way. For instance - if I return a JavaScript object for Degreesthat looks something like the following: {degrees: [{id: 1, title: "PhD"}]}, then this converts my degreecollection into a flat array. Here is my code:

fetch()在模型中调用该方法时遇到的问题是,基本数组和模型中的集合对象之间存在各种阻抗不匹配。由于parse根据定义应该返回要使用的哈希set值而不是实际设置值,因此我不可能以这种方式设置我的集合。例如 - 如果我返回一个Degrees类似于以下内容的 JavaScript 对象:{degrees: [{id: 1, title: "PhD"}]},那么这会将我的degree集合转换为平面数组。这是我的代码:

window.Models.Member = Backbone.Model.extend({

    url: '/api/member',

    initialize: function() {

        _.bindAll(this);

        this.set('degrees', new window.Collections.DegreesList());

        this.fetch();

    },

    parse: function(response) {

        var setHash = {};

        setHash.first_name = response.first_name;
        setHash.last_name = response.last_name;
        setHash.office_phone = response.office_phone;

        // When this is run, this.get('degrees') will now return a flat array rather than a DegreesList collection
        setHash.degrees = _(response.degrees).each(function(item) { 
                return {id: item.id, title: item.title} 
        });

        return setHash;

    }

});

I could manually set the collections in my parse function, but that seems like its subverting the Backbone way, and hacky.

我可以在我的 parse 函数中手动设置集合,但这似乎颠覆了 Backbone 方式,而且很糟糕。

EDIT: I've temporarily solved the problem by doing the following:

编辑:我通过执行以下操作暂时解决了这个问题:

parse: function(response) {

    var setHash = {};

    setHash.first_name = response.first_name;
    setHash.last_name = response.last_name;
    setHash.office_phone = response.office_phone;

    this.get('degrees').reset( response.degrees );

    return setHash;     
}

I doubt this is the optimal solution, but it certainly works for now. I'm open to better suggestions.

我怀疑这是最佳解决方案,但它现在肯定有效。我愿意接受更好的建议。

采纳答案by fguillen

I use exactly the same workaround you are proposing except with a few changes.

我使用与您提议的完全相同的解决方法,但有一些更改。

  1. To avoid misunderstanding I call the collection in the JSON: degreesData
  2. In the initializeI create the collection like this: this.degrees = new Degrees().reset( this.get( "degreesData" ) );
  1. 为了避免误解,我在 JSON 中调用集合: degreesData
  2. initialize我创建这样的集合:this.degrees = new Degrees().reset( this.get( "degreesData" ) );