I have a problem.
I have 2 store files (1 event and 1 places). I'm loading these stores from a database. Now the problem is that in the event store there is a field called {id_place}. This field contains a number which is also a number in the places store. Now what I need to do is get the name from the place of the {id_place}. So the {name} is a field in the places store. So what I need is a function to get a record from another store and then get the {name} from that store. So I send the id_place to the placestore, then I need to find the record with that id and I need to get the field {name} from that record. I don't know if I'm making sense, it's kinda hard to explain. I hope this can be done.
Thanks in advance!
Edit:
showPlace: function(id_place){
var placeUrl = 'http://admin.hishanghai.info/sencha/places.php?action=read&callback=callback&id=' + id_place;
store = new Ext.data.Store({
model: 'android.model.Placesmodel',
autoLoad: true,
proxy: {
type: 'scripttag',
url: placeUrl,
reader: {
type: 'json',
rootProperty: 'place'
},
extraParams: {
action: 'read'
}
}
})
//var naam = Ext.getStore('Placesstore').getById('id_place').get('name')
//var Record = store.getAt(id_place);
//var naam = store.getById('id_place').get('name');
var naam = store.get('name');
return naam;
}
This code creates a store with the info from 1 record. Now I only need to access the name field. how do i do that?
found it:
showPlace: function(id_place){
var placesstore = Ext.getStore('Placesstore');
var i =0;
var placeid = 0;
var aRecord;
while(placeid != id_place){
aRecord = placesstore.getAt(i);
placeid = aRecord.data.id;
i+=1;
}
var naam = aRecord.data.name;
return naam;
}
Related
I have created some models all saved in local storage collection. My challenge is how to delete a model fom the collection in local storage using one of the model attributes value. Below is extract of my code:
var Food = Backbone.Model.extend({
defaults: {
title: '',
calories: 0,
date: '',
history: false
}
});
var Foods = Backbone.Collection.extend({
model: Food,
localStorage: new Backbone.LocalStorage('Health-Tracker')
});
my_foods = new Foods({});
var ls = new Backbone.LocalStorage('Health-Tracker');
var res = ls.findAll();
var n = res.length;
var i, id;
for (i = 0; i < n; i++) {
food = res[i];
id = food['id'];
title = food['title'];
calories = food['calories'];
date = food['date'];
history = food['history'];
//I am stuck here
if (history) {
ls.remove(food); // Not working
// I have also tried this
ls.remove(id); // not working
}
}
Note that history is set to true successfully for some models in other section of the bigger codes. Above is only where I need to take action if history value is true. And it is bad code for now.
Any assistance is appreciated
Note that this app does not use server to store/retrieve data. All models are stored in the local storage. It is a very long process to use findAll() and remove() as I used above to delete from the localstorage. The following was added to delete models from the local storage:
my_foods.fetch();
my_foods.models.forEach(function(food){
day = food.get('day');
new_day = get_new_day();// this function isn't included above
if (new_day > day) {
food.destroy();
}
});
I'm trying to get some data from the node server, which works fine, but when I try to GET data via the Backbone fetch (or sync), the request fails. I noticed that, for some reason, actual request is wrong: 'GET http://localhost:3000/socket.io/1/' where it should be 'GET http://localhost:3000/cars', since '/cars' is the value of the URL field that Backbone uses by convention for these operations. These are the relevant modules:
var Backbone = require("backbone");
var Car = require('models/car');
var Cars = Backbone.Collection.extend ({
model: Car,
url: '/cars',
// Unselect all Car Cards
resetSelected: function() {
for (var i=1; i<=this.length; ++i) {
var carcard=this.get(i);
carcard.set({"selected": false});
console.log(carcard.attributes.name + ' unselected');
}
},
// Select a specific model from the collection
selectByID: function(id) {
this.resetSelected();
var carcard = this.get(id);
carcard.set({"selected": true});
console.log(carcard.attributes.name + " selected");
return carcard.attributes.id;
}
});
module.exports = Cars;
And a model:
var Backbone = require("backbone");
var Car = Backbone.Model.extend({
defaults: {
year: 2011,
brand: "Brand",
model: "Model",
name: "Car Name",
pictureFle: "img/car.jpg",
kmTraveled: 0,
litresSpent: 0,
selected: false
},
});
module.exports = Car;
I tried to populate the collection like this:
var cars = new Cars();
cars.fetch();
but, as I explained, failed. Any ideas what the problem could be?
I have an extjs store associated with grid class. when I select a class, it gives ClassID of the record.
var ClassData = record.get('ClassID');
console.log(ClassData);
Based on this ClassID I am loading store of next grid:
var Grid = this.getSemGrid();
var Store = Grid.getStore(); // your grid's store
//load store with records having selected class ID
var g = Store.load( {params : {ClassID: ClassData }});
Till here everything is fine.
Once the store is loaded, I am getting all loaded records (Error Area)
var selected = g.getRange(); // getRange = select all records
Then pushing all the values of one field of all records in an array
var Excerpt = []; // start with empty array
Ext.each(selected, function(item) {
// add the fields that you want to include
var Obj = {
third_field: item.get('ExamName')
};
Excerpt.push(Obj); // push this to the array
}, this);
console.log(Excerpt);
Excerpt gives array of previously selected record not the current record.
I have also tried
Store.loadData([],false);
to clear all the loaded data of store before loading it again.
Got this working
var g = Store.load({
params : {ClassID: ClassData },
callback : function(records, operation, success){
var Excerpt = []; // start with empty array
Ext.each(records, function(item) {
// add the fields that you want to include
var Obj = {
third_field: item.get('ExamName')
};
Excerpt.push(Obj); // push this to the array
}, this);
}
});
If I have a record, which belongs to -a- store, but without knowledge of -which- store it belongs to, how do I delete that record?
Eg
var store = Ext.create('Ext.data.Store',{
model:'Pies'
data:{Type:123,Name:"apple"}
})
var record = store.getAt(0)
//How do I store.remove(record); without actually having the store record handy?
Your record will actually have property .store which you can use to reference store it belongs to - http://docs.sencha.com/ext-js/4-0/#!/api/Ext.data.Model-property-store
Here is an example of Ext JS code that removes a given record. The record has a reference to the store it belongs to. Using that store reference combined with the store's remove method, you can remove the record as coded below.
Run the code pasted below here:
http://jsfiddle.net/MSXdg/
The example code:
Ext.define('Pies', {
extend: 'Ext.data.Model',
fields: [
'Type',
'Name'
]
})
var pieData = [{
Type:123,
Name:'apple'
}];
var store = Ext.create('Ext.data.Store',{
model:'Pies',
data: pieData,
proxy: {
type: 'memory'
}
})
var debug = Ext.fly('debug');
if (debug) {
debug.setHTML('Record count: ' + store.getCount());
}
console.log('Record count: ' + store.getCount())
var record = store.getAt(0);
// remove the record
record.store.remove(record);
// display the store count to confirm removal
if (debug) {
debug.setHTML(debug.getHTML() + '<br />Record count after removal: ' + store.getCount());
}
console.log('Record count after removal: ', store.getCount())
Ive created a store, which I can manipulate.
var storeJ = new Ext.data.JsonStore({
root: 'data',
idProperty: primaryKey,
id:'storeJ',
fields: [primaryKey,'name', 'code', 'email'],
autoSave:true,
storeId: 'storeJ',
proxy: proxyJ,
writer: writer,
});
storeJ.load();
When I try to add to this store, via a button, I return a Pk ( via php )
This happens after
storeJ.insert(0, r);
//Tbar Item
handler: function(){
var r = new storeJ.recordType({ });
grid.stopEditing();
storeJ.insert(0, r);
grid.startEditing(0, 1);
}
I assume I need some kinda listener to get there, but how on earth do I create it?
You already have the record before the insert.
Will something like the following work:
var r = new storeJ.recordType({ });
r.data[primaryKey] = pk;
grid.stopEditing();
storeJ.insert(0, r);
grid.startEditing(0, 1);
ensure you pass a object in your return, and that the root of the store is correctly pointed to the right object.
Simply returning 1 or so won't work, you need to return json_encode($return['data']=>1); or its equivalent in the above defined store.