Reputation: 507
I have a model something like this and I want to access those object's key's values in a jquery template
var Item = Backbone.Model.extend({
defaults : {
name : "abc"
designation: "def"
},
defaults : {
foo: function (obj) {
return {
child1: {
childName: "Mike",
childAge: 20
},
child2: {
childName: "Tom",
childAge: 10
}
};
}
}
});
And a jquery template where I want to access those values something like this
<script type="text/x-jquery-tmpl" id="temp1">
{{ if child1.childAge > 18 }}
<p> Allowed </p>
{{ else }}
<p> Not Allowed </p>
{{ /if }}
</script>
I tried this.model.toJSON()
but the I'm not able to access those object values like child1.childName
like I can access "name" and "designation"
How can I access those?
Upvotes: 0
Views: 33
Reputation: 25178
You can not have two defaults
properties in your object and a ,
is missing after name : "abc"
.
defaults
will copy to your instante the return value of a function if is defined in this way, or the properties of an object if is defined in this way. But if you your object have functions, it will be copied as is, not executed, and copying the return values.
I'm not sure what exactly you are trying to achieve, at it seems that you must work with collection and nested models.
But answering your actual question, this should workd:
const Item = Backbone.Model.extend({
defaults : function() {
return {
name: "abc",
designation: "def",
child1: {
childName: "Mike",
childAge: 20
},
child2: {
childName: "Tom",
childAge: 10
}
}
},
});
const item = new Item()
console.log(item.toJSON())
Upvotes: 0