I'm trying to create model objects on the fly using Backbone.Collection.create...but I note that the collection uses its url and not the model's url...
Is this how it suppose to work? Can I override it on the fly just for this particular .create()?
You can specify the options attribute that contain the url property :
Backbone.Collection.create({
key: "value",
...
}, { url : 'yourUrlHere' });
Related
When I require to add a private property to an object (for view or logic control) that will be submitted to a rest api latter, is valid prefix the property with $$? This is tricky in cases when I have an object with a list of children and each child requires a private property that should not be sent.
{
name: 'my object',
items: [
{
name: 'my child',
$$editing: true
},
{
name: 'my other child',
$$editing: true
}
]
}
Yes, angularjs $http service uses the angular.toJson method by default.
All properties with $$ are filtered out, because angular uses such properties internally. (e.g. you may have seen the $$hashKey property, which is added by angular)
You can try:
console.log(angular.toJson({a:1, $$b:2, c: {x:2,$$_y:3}}))
results in "{"a":1,"c":{"x":2}}"
How can I extract the context of the component to which formula is bound? I want to get the context of label1 in the get() of formula1.
I have a view component
{
xtype : 'label',
name : 'label1'
bind : {
text : '{formula1}'
}
}
in view model
formula : {
formula1 : {
get : function(param){
//------------------ how to get the name of the label
here to which this formula is bound ------
}
}
}
Theoretically, you could use ComponentQuery and find the component you need but I advice strongly against this approach. Much better is to bind also name. Then you can use this.getName() to access value 'label1'.
I have the following situation. I have a TreeStore that is synced with my server. The server response returns the modified node data with an additional property 'additionalTasks' (which stores some server-generated info for new nodes). How can I get this property's value if all of the store's listeners get already an instantiated record object and not the raw response data ? I've tried adding this additional field to my model, but when I check it's value for the records in the listener function they're shown as null.
Ext.define('ExtendedTask', {
extend: 'Ext.data.NodeInterface',
fields: [
{ name : 'additionalData', type : 'auto', defaultValue : null, persist : false}
]
});
var store = Ext.create("Ext.data.TreeStore", {
model : 'ExtendedTask',
autoSync: false,
proxy : {
method: 'GET',
type : 'ajax',
api: {
read : 'tasks.js',
update : 'update.php',
create : 'update.php'
},
reader : {
type : 'json'
}
},
listeners: {
update : function(){
console.log('arguments: ', arguments);
}
}
});
And this is my update.php response :
<?php
echo '[ { "Id" : 1,'.
'"Name" : "Planning Updated",'.
'"expanded" : true,'.
'"additionalData": ['.
' {'.
' "Name" : "T100",'.
' "parentId" : null'.
' }'.
']'.
']';
?>
The store's proxy always keeps the raw response from the most recent request. Try something like this and see if the information you need is there.
update : function(){
console.log(this.proxy.reader.rawData);
}
I've faced the same issue. What I ended up having to do was use a standard tree model property (I used cls) for whatever custom data you are trying to load into the model. I could be wrong but from the time I took looking to this issue, it seems that extjs is forcing a tree model to only use the standard fields. They state:
If no Model is specified, an implicit model will be created that implements Ext.data.NodeInterface. The standard Tree fields will also be copied onto the Model for maintaining their state. These fields are listed in the Ext.data.NodeInterface documentation.
However from testing it seems that only the standard fields are available regardless of if a model is specified. Only workaround I could find was to use an a standard string type field which I didn't need like cls, though I'd be interested to see if anyone has found a better way.
http://docs.sencha.com/ext-js/4-1/#!/api/Ext.data.TreeStore
I have a model, and when I do model.attributes.model I see the attributes of the model. One attribute is name, so model.attributes.model.name returns the right name. However, when I do model.get('name') I get the default that I had set in the model.
How do I set all the attributes of the model so that it works with get?
JSON used to build the model
[{
"model":{
"name":"My name",
"description":
"Description goes here!",
"vote_score":null
},
"context":{}
}]
Either modify your server-side code to return an array of model attributes, as you said in the comments, or modify your model definition to override the parse method :
var MyModel=Backbone.Model.extend({
parse: function(data) {
return data.model;
}
});
My backbone.js model has a json object in defaults like this:
test: {
testArr: [{
obj1: value1,
obj2: value2
}]
}
How can I update just a part of the model for eg: test.testArr[0].obj1 using this.model.set()?
You'll need to extract the array from the properties, update it, and inject it back in. Assuming your model is called model, that would look like:
var newTestArr = model.get('testArr');
newTestArr.obj1 = 'foobar';
model.set({testArr: newTestArr});