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"]]
? Isfilters
a global variable?filters
orFilters
? – 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
1 Answer
Reset to default 7You 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条)