Store array Data Object in localStorage using sencha touch - javascript

Using sencha touch, I want to store my array list data in localStorage as shown in image.
onStore refresh function as shown in image below
I stored simple data as Like Date, Id, Name in LocalStorage on BookStore refresh function. But could not find the way to store arrayObject directly to localStorage.

I am not sure i understand your question completely..
If you want store javascript object in local Store, you can do it by converting javascript object into string using JSON.stringify() method.
var testString = JSON.stringify(testObj); //Converts testObject to Json string.
localStorage.testString = testString; // Stores testString into local storage.
You can get testString from local storage and convert it into javascript object by
var testObj = JSON.parse(localStorage.testString);
Or you can use
Ext.encode(); // Encodes an Object, Array to String.
Ext.decode(); // Decodes (parses) a JSON string to an object.

you can store full object but if you just want to store subjects then you need to create new model.
define a model for storing subjects or you can store the full object you are getting.
Ext.define('Demo.model.Subjects', {
extend: 'Ext.data.Model',
config: {
fields: [
{ name: 'id', type: 'int' },
{ name: 'Subjects', type: 'auto' }
]
}
});
define a local store
Ext.define('Demo.store.MySubjectLocalStore', {
extend: 'Ext.data.Store',
config: {
storeId: 'mySubjectLocalStore',
model: 'Demo.model.Subjects',
autoLoad: true,
clearOnPageLoad: true,
proxy: {
type: 'localstorage',
id: 'demo-mySubjects-local-store'
}
}
});
to store data in local store
var subjectsLocal = Ext.getStore('mySubjectLocalStore');
//loop to get your subjects array from main Object then add each array to localstore
//loop start
var subjects = //get it from parent object inside loop
var subjectsModel = Ext.create('Demo.model.Subjects', {
Subjects : subjects,
id: //use integer so that you can get by this id when you retrive it
});
subjectsLocal.add(subjectsModel);
//loop End
subjectsLocal.sync();

Related

Trying to set a value by referencing separate json

Im trying to separate out the functionality of my model and the data so ive created a separate json file with a basic table
when my model builds it creates an object and i need it to create a value in it based on a value coming in:
{
"1":"apple",
"2":"banana",
"3":"orange",
"4":"grape"
}
async save (xmlOrder) {
let customerOrder = {
ID: xmlOrder.ID,
Name: xmlOrder.Name ,
ItemCode: xmlOrder.ItemCode ,
Fruit: (This set by referencing the json, based on the Item code coming in above)enter code here
}
You can import that json object in file where you're having your model, than based on input to function you can get value out of object.
let obj = {"1":"apple","2":"banana","3":"orange","4":"grape"}
function save (xmlOrder) {
let customerOrder = {
ID: xmlOrder.ID,
Name: xmlOrder.Name ,
ItemCode: xmlOrder.ItemCode ,
Fruit: obj[xmlOrder.ItemCode] || 'Not in list',
}
return customerOrder
}
console.log(save({ID:33,Name:'Name',ItemCode:'2'}))
console.log(save({ID:303,Name:'Name1',ItemCode:'21'}))

How to get data from xml by using URL

I have a grid where I am loading the data from the data store. Bu using some url. Code is something like that.
var store = new Ext.data.Store({
proxy : new Ext.data.HttpProxy({
method: 'GET',
url: SomeURL
}),
remoteSort:true,
method: 'GET',
baseParams: {start: 0, limit: 25},
sortInfo:{
field: 'UPDATEDON',
direction: 'DESC'
},
reader: new Ext.data.XmlReader({
totalProperty:'#Total',
record: 'ITEM'
}, [ {name:'name',type:'string', mapping:'#name',SortType:"asUCString"},
{name:'value', mapping:'#value'}
]),
grid:this,
listener : {
load:function(){
}
}
This is loading correctly. No problem in grid. I need to access grid data at some other place in my code. so for that I am call
`var xyz = new MyGrid({val:1,newval:2});`
val and newVal are the two para for grid. But I am not getting store and store value here. Is there anyway I can get store here or get the xml value by using the same url again.
You can set in your store autoLoad: true - if it is already binded/set with this new Grid.
Or just say xyz.store.reload();
Reloads the store using the last options passed to the load method.
You can get the data form xyz.store.
How to get the information from a store depends on you and what you have there stored. You can use 'find', 'findBy', 'each' and so on (senscha doc extjs V3.4)

firebase $add() .push() .set()

I am using firebase, and angularfire.
there are so many ways to do CRUD with the Firebase Api
actually, I still don't get what is specific difference for using
$add with $firebaseArray
.push() method
.set() method
I think they are technically same, I prefer to use .set method() without knowing the exact reason, why I'd using that. is there any specific reason to not use it? what is exactly $firebaseArray did? if we could just declare basic reference variable.
in this case:
var usersRef = Ref.child('users');
$scope.createUser = function() {
$scope.userRef.child($id).set({
name: name
});
};
or
$scope.data = $firebaseArray(Ref.child('users'));
$scope.createUser = function() {
$scope.data.child($id).$add({
name: name
});
};
thank you.
If I have the following data tree in Firebase:
{
users:
{
key: { name:"bob" }
}
}
When I do an $add, I will create a new item in the tree
$scope.data.child('users').$add({
name: name
});
Since $add uses the Push method in Firebase, new random Key will be used when pushing data to the child.
{
users:
{[
key: { name:"bob" },
key2: { name:"name" }
]}
}
If I do a set on the same Users object, I will overwrite the data that is already there. So, in your example, without specifying a key, you will overwrite the entire user object.
$scope.userRef.child('users').set({
name: name
});
};
This will result with this data
{
users:
{
name: "name"
}
}
This happens because any null values you pass to the Set method will delete any data that was originally there.
Passing null to set() will remove the data at the specified location.
https://www.firebase.com/docs/web/api/firebase/set.html

dynamic json to combobox dojo

I have a problem with filling a Combobox dynamic with a jsonRest from a cross Origin request.
I found a chance to do it static (I hope this is the right vocabulary), but I don’t figured out how to do it for more than one case.Because this is a little part of a bigger Website with five Comboboxes.
Here's the Code
require([
"dojo/_base/array",
"dojo/store/Memory",
"dojo/store/JsonRest",
"dijit/form/ComboBox",
"dojo/store/Cache",
"dojo/store/Observable",
"dijit/form/Textarea",
"dojo/domReady!"
],
function(array, Memory, JsonRest, ComboBox, Cache, Observable, Textarea){
var myArray = new Array;
var myStore = new Observable (new Cache (new JsonRest ({
target: “URL / target”,
idProperty: "WA",
headers: { "X-Requested-With": "" }
}), new Memory ()));
var myTextarea = new Textarea ({
name: "myarea",
style: "width:200px;"
}, "myarea");
myStore.query().then(function(response){
});
store = new Memory({data: myArray}); //Store anlegen ... mit Array befüllen
var comboBoxWA = new ComboBox({
id: "comboWA",
name: "WA",
value: "",
store: store, // übergabe angelegter Store zu Combobox
searchAttr: "WA"
}, "comboWA");
// Array befüllen,.. Store anlegen,... Array dem Store zuweisen
myStore.query().then(function(response){
dojo.forEach( response, function( obj ) {
for (var p in obj) {
if(p=="WA"){
//Here is my Problem, I can’t change the “WA” in myArray.push to some global Variable.
myArray.push({"WA" : obj[p]}); //Array befüllen
console.debug(myArray.toSource());
}}
});
});
});
The json response looks like this
[Object { WA=‘'WA_30_14"}, Object { WA="WA_30_12"} , Object { WA="WA_30_10"}, Object { WA="WA_30_16"},…]
Have anybody an Idea or a simple example for me?
Thanks, Georg
You can also try the below method. so you require "myArray.push({"WA" : obj[p]});" to be "myArray.push({some_global_variable: obj[p]});".
do the following.
1.create an empty LOCAL object before the push method
2.Use the array syntax for assiging the property to the local variable
3.Pass the local variable as parameter to the push method.
var localobj = {}; // step 1
localobj[global_var] = obj[p]; //step 2
myArray.push(localobj); // step 3
You can check the value of the global variable before using push() using
console.log("My glogbal varible"+global_var);

ExtJS creating record from existing store

I have created an ext store like so:
var store = new Ext.data.JsonStore({
root: 'vars',
fields: [{ name: 'rec_id', mapping: 'rec' }, { name: 'identity', mapping: 'id'}]
});
This works alright when I add data to the store via loadData(); and some json which looks like:
{ vars : {rec: '1', id:'John'} }
My problem is that if I use add(); to get this record into the store I have to first create it as an Ext.data.Record object.
I do this as pointed out here: https://stackoverflow.com/a/7828701/1749630 and it works ok.
The issue I have is that the records are entered with their mapped parameters rather than the ones I set. I.e, 'rec_id' becomes 'rec' and 'identity' becomes 'id'.
What am I doing wrong here?
You need to do the mapping manually, something like this:
var myNewRecord = new store.recordType({
rec_id: vars.rec,
identity: vars.id
});
store.add(myNewRecord);

Categories