javascript - Update backbone.js model with new array element - Stack Overflow

I have a backbone.js model similar to the one shown below.Filters = Backbone.Model.extend({defaults : {

I have a backbone.js model similar to the one shown below.

Filters = Backbone.Model.extend({
    defaults : {
        title: [ ["title1", "easy"], ["title2", "hard"] ]
    }
});

I'm trying to add an element to the first-level array, such that the model then bees:

Filters = Backbone.Model.extend({
    defaults : {
        title: [ ["title1", "easy"], ["title2", "hard"], ["title3", "medium"] ]
    }
});

The code I have right now is this:

function setFilters() { 
    var options = {};   

    for (var facet in facets) {
        for (var facetKey in facets[facet]) {
            if (!filterExists(facetKey)) {
                options[facetKey] = new Array(new Array(facets[facet][facetKey], "equals"));
            }
            else {
                (filters[facetKey]).push(new Array(facets[facet][facetKey], "equals"));
            }
        }
    }

    filters.set(options);
}

The function filterExists simply checks if the key "title" is present in the model. When I run this, it says that filters[facetKey] is undefined. But isn't this the first-level array I need to push my element into?

I have a backbone.js model similar to the one shown below.

Filters = Backbone.Model.extend({
    defaults : {
        title: [ ["title1", "easy"], ["title2", "hard"] ]
    }
});

I'm trying to add an element to the first-level array, such that the model then bees:

Filters = Backbone.Model.extend({
    defaults : {
        title: [ ["title1", "easy"], ["title2", "hard"], ["title3", "medium"] ]
    }
});

The code I have right now is this:

function setFilters() { 
    var options = {};   

    for (var facet in facets) {
        for (var facetKey in facets[facet]) {
            if (!filterExists(facetKey)) {
                options[facetKey] = new Array(new Array(facets[facet][facetKey], "equals"));
            }
            else {
                (filters[facetKey]).push(new Array(facets[facet][facetKey], "equals"));
            }
        }
    }

    filters.set(options);
}

The function filterExists simply checks if the key "title" is present in the model. When I run this, it says that filters[facetKey] is undefined. But isn't this the first-level array I need to push my element into?

Share edited Feb 20, 2012 at 18:56 kaspnord asked Feb 20, 2012 at 17:02 kaspnordkaspnord 1,4832 gold badges19 silver badges28 bronze badges 4
  • 1 Offtopic: instead of new Array(new Array(facets[facet][facetKey], "equals")) why not you write the much shorter [[ facets[facet][facetKey], "equals"]]? Is filters a global variable? filters or Filters? – biziclop Commented Feb 20, 2012 at 17:22
  • @biziclop yes, filters is a global variable. Thanks for the tip on the arrays. – kaspnord Commented Feb 20, 2012 at 17:24
  • Also { {"title1", "easy"}, {"title2", "hard"} } should be [ ["title1", "easy"], ["title2", "hard"] ]. {} is for creating objects: { name:'Joe', city:'London' } – biziclop Commented Feb 20, 2012 at 17:32
  • @biziclop, yes I'll change that now to prevent confusion in the future. – kaspnord Commented Feb 20, 2012 at 18:56
Add a ment  | 

1 Answer 1

Reset to default 7

You can access model attributes with .get() and .set() functions, or directly via the .attributes property:

http://documentcloud.github./backbone/#Model-attributes

var filters = new Filters();
filters.attributes.facetKey.push( [...] );

OR

filters.set('facetKey', ( filters.get('facetKey') || []).concat([...]));

Anyway, here is your transformed function which may or may not work:

function setFilters() { 
    for (var facet in facets) {
        for (var facetKey in facets[facet]) {
            var f = [ facets[facet][facetKey], "equals" ];

            if( filterExists(facetKey)) {
            // OR: if( filters.attributes[ facetKey ]){
                filters.attributes[ facetKey ].push( f );
            }else{
                filters.attributes[ facetKey ] = [ f ];
            }
        }
    }

    // trigger change event for all attributes
    filters.set( filters.attributes );
}

Bonus:

(filters.attributes[ facetKey ] = filters.attributes[ facetKey ] || [] ).push(f);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744885286a4599080.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信