How to set multiple values at one time to Ext.data.Model? - extjs

In Ext.data.Model class we have set(fieldName, newValue) method which sets one model field to the given value.
How to set multiple values at one time? Something like:
Ext.data.Model.set({
field1: 'value1',
field2: 345,
field3: true
});

http://docs.sencha.com/extjs/4.2.1/#!/api/Ext.data.Model
You can set the field to set, or an object containing key/value pairs
record.set({
field1: value,
field2: value2
});
// or
record.set(field1, value);
record.set(field2, value2);

That's not possible, but as per my understanding you only want to merge this different calls for setting a value in model when you need to notify about changes in model only once to store so store will only fire update event once
If this is the case for you then you can use beginEdit and endEdit functions
var model = new Ext.data.Model();
model.beginEdit();
model.set('field1', 'value1');
model.set('field2', 345);
model.set('field3', true);
model.endEdit();

Just create a new record which returns a blank record with all the model fields
//this one is for the grid model
var blank_record = Ext.create(grid.store.model);
Create an object to set the new values to fields
var new_record = {
'field1':'value1',
'field2':'value2',
'field3':'value3'
};
then set the object values to the blank record
blank_record.set(new_record);
Further if u want to add the newly created record to the grid
grid.store.add(blank_record);
Hope this helps

I'm late to the party but here's another way to do it:
var rec = new Ext.data.Model({
field1: 'value1',
field2: 'value2',
field3: 'value3'
});

Can't you do this with just Ext.create() ?
As explained here:
http://docs.sencha.com/extjs/4.1.3/#!/api/Ext.ModelManager-method-create

Related

x-editable: customize an x-editable for having more field, but only one editable

in these days I'm going crazy about x-editables...I can't understand how customize an x-editable to obtain a complex array of fields.
I need to create an x-editable customized, that has this behavior: a checklist (so an array of value) but with an input text field for every check item.
I need something like this (simulated with paint shop)
This x-editable when submit must produce an output like (just a retouched example)
How can I create an x-editable with this behaviour/functionality?
Pratically a collection of objects, where the object have two fields: id, number of fruit.
Thanks in advance to all.
Following comments, check this solution (referred to fiddle):
$('#save').on('click', function(){
var fruits = [];
$('.editable').each(function(i, v){
/* store it as you like */
var name = $(v).data('name');
var value = $(v).editable('getValue'); //x-editable stores value under 'name' index, like {"banana": 10}
var fruit = {"fruit_id": name, "num": value[name]}
fruits.push(fruit);
}).promise().done(function(){
// promise() will wait .each() loop to finish before going ahead
$.ajax({
url: "/post",
data: {fruits: fruits},
success: frunction(data){
/*....*/
}
});
});
});
Fiddle

How to add new items to an array in MongoDB

I'm trying to add a new item to whichever name that was passed in under whichever id. My first problem is that it seems like its not grabbing the values from any of my variables (name, item, id), instead just using them as object keys. My next issue is that when I tested by hard-coding sample table info onto here, it wasn't adding more items to that array, but simply replacing the entire array with whatever values I had here.
function addlist(name, item, id){ // Add to user's list
console.log(id);
db.collection('newcon').update({_id: id}, { "$set": { "$push": { name:item } } });
ret(id);
}
$set is not an array update operation.
The $set operator replaces the value of a field with the specified value.
You just want to use $push by itself, as in
.update({_id: id}, {$push: {name: item}})
You can't interpolate object property names in raw object declarations, so if you want to use a variable name you will have to create an object to do this:
var obj = {};
obj[name] = item;
You can then pass this to .update, {$push: obj}

Backbone Collection get property

Got a server returning a JSON object like so:
{
'key1':'value'
'key2':{
'key2_0':'value'
}
}
And a collection:
var Collection = Backbone.Collection.extend({
url:api.url//which returns the object above
});
var collection = new Collection();
collection.fetch({
success:function(data){
//do something
}
});
Now i need to use certain properties of the collection throughout my application, but say i need key1, i always have to do collection.at(0).get('key1');//returns 'value', because the data returned is stored within the collection, in a new Array at key 0.
Question:
How to directly... collection.get('key1')//now returns undefined... because it is.
I know i could expose an object to the global scope in the collection success function some_other_var = data.toJSON()[0] and access the some_other_var properties directly, but that's not what i'm looking for;
In order to use the get() function from a Backbone.Collection you need to know the model id or cid wanted.
For instance, lets say your data coming from the server is like follow:
[{
id: '123',
name: 'Alex'
}, {
id: '456',
name: 'Jhon'
}]
In that case you can do this:
this.collection.get('123').get('name') // Return "Alex"
Keep in mind that collection is just a set of model, so behind the scenes by doing collection.get() you are getting a model
Tip: If you don't have any kind of id in your server data, there is always the option of using underscore methods:
find
filter
some
contains
etc
It seems like you're trying to ascribe attributes to a collection, but a collection is merely a set of models. Having additional data that is constant throughout the collection suggests that it should be wrapped inside another Model, which is demonstrated here: Persisting & loading metadata in a backbone.js collection

Backbone.js model: attribute of type array maintains data between instances

Suppose I have following Backbone.js model:
class App.Models.Article extends Backbone.Model
defaluts:
id: ''
name: ''
tags: []
and a view to display its contents. In a master view I create an instance of article view by passing a newly created Article as :
v = new App.Views.ArticleView({ model: new App.Models.Article() })
and render it to the page.
In the article view and by user interaction some tags are added to the tags array by following code:
addTag: ->
tags = #model.get('tags')
tags.push({id: '', name: 'foo'})
tags.push({id: '', name: 'bar'})
So far so good! Then I close the article view and render another view. Later I want to again render the article view so the code:
v = new App.Views.ArticleView({ model: new App.Models.Article() })
runs again.
The problem is after rendering, I still can see previously entered tags in the tags array i.e. if inside articles view, I write:
console.log(#model.get('tags'))
it will output two objects added before. I expect that each instance has its default values when created not something that there is no more reference to it. Any ideas? Any issues with Coffescript?
P.S. I hope the problem statement is clear enough :)
Don't use arrays and objects in defaults, they will get shared across model instances. If you want tags to be empty array, add that in initialize function.
var MyModel = Backbone.Model.extend({
initialize:function(){
this.set('tags', [])
}
})
Solved!
The problem is that Coffeescript attaches default to the prototype, so defining the defaults as follows solves the problem:
defaults: ->
id: ''
name: ''
tags: []
Again it is attached to the prototype but as a function, it returns an empty object for each call.

Why do I get the error in extjs, "gridstore is not a constructor"?

I have a button with a handler. In this handler I call for this function:
onAddClick = function() {
gridStore = Ext.getStore('gridEdit');
var rec = new gridStore({
stopCode: '',
stopOrder: '',
stopId: ''
}), edit = this.editing;
edit.cancelEdit();
this.store.insert(0, rec);
edit.startEditByPosition({
row: 0,
column: 1
});
};
When I click on the button it will give the error message: "gridStore is not a constructor". Why is this?
You have a lot of errors in the code, first of all, once you use Ext.getStore, it returns an store with that id (the store is already created). So the gridStore (global variable, since you haven't use var) will point to that store.. So no need for new.
If you want to add custom config, just use Ext.create().
The var 'rec' .. should that supposed to be a record from the store?
I don't know what you are trying to do exactly but:
-gridStore is the actual store;
-a record can be created by modelManager or by Ext.create and the model class.

Resources