Sencha Touch Changing/updating the store values - javascript

Hi I have a store with name and values, I want to change/update the values of the store based on the user selection in settings screen.
suppose I have data like this
data: [
{firstName: 'Tom',value:1},
{firstName: 'Ed',value:2},
{firstName: 'Jam',value:3},
{firstName: 'Aar',value:4},
{firstName: 'Dav',value:5},
{firstName: 'Mic',value:6}
]
When user changes settings (I am storing this change of settings in global variable), all values should change to multiples of 2 as below
and update my view with these new values.
data: [
{firstName: 'Tom',value:2},
{firstName: 'Ed',value:4},
{firstName: 'Jam',value:6},
{firstName: 'Aar',value:8},
{firstName: 'Dav',value:10},
{firstName: 'Mic',value:12}
]
I need some idea how to implement this and best way to do it.

After change setting, try this
var store = Ext.getStore('StoreName');
store.removeAll();
store.add(newData);

each function of store solved my problem
var store = Ext.getStore('StoreName');
if(settings changed condition){
store.each(function(record) {
record.set('value', (record.get('value')* 2));
record.dirty = true;
});
store.sync();
}

Related

TypeORM AND Find Option

I have products with a price, I want to find products whose price will be less than 10 but more than 5. So that SQL query looks like this:
SELECT * FROM products WHERE products.price < 10 AND products.price > 5
Is it possible to do this without using Query Builder?
I don't find the And operator in the documentation
The better way is to use Between Operator in your case. Between(1, 10) 1<x<10
You just need to use where operator.
userRepository.find({ where: { firstName: "Timber", lastName: "Saw" } });
This code executes this query:
SELECT * FROM "user" WHERE "firstName" = 'Timber' AND "lastName" = 'Saw'
And if you want to use OR, you use an array of conditions:
userRepository.find({
where: [
{ firstName: "Timber", lastName: "Saw" },
{ firstName: "Stan", lastName: "Lee" },
],
});
This code will executes this query:
SELECT * FROM "user" WHERE ("firstName" = 'Timber' AND "lastName" = 'Saw') OR ("firstName" = 'Stan' AND "lastName" = 'Lee')
More infos in the doc: https://github.com/typeorm/typeorm/blob/master/docs/find-options.md
These docs clarify how to use find like SQL
https://orkhan.gitbook.io/typeorm/docs/find-options
Note that for conditional where and other statements, you need QueryBuilder.

Problem with Child value from Vue component changing Parent value

I have an Array that I use to populate a Dialog Vue Component. I have:
basicInfo: [{ firstName: "John" , lastName: "Doe" }],
<basicInfoForm v-model="showBasicInfoForm" :basicInfo="newbasicInfo[0]"></basicInfoForm>
on the Parent I have
{{basicInfo[0].firstName + ' ' + basicInfo[0].lastName}}
I have a button that will call a Form Component and open a Dialog popup that allows editing. My problem is that all changes to the Dialog are also immediately shown on the Parent. I would like to have on the Child Dialog a cancel button so no changes will be made. I cloned the array before I pass it:
this.newbasicInfo = this.basicInfo.slice();
and on the Child Dialog
<v-text-field
v-model="basicInfo.firstName"
label="First Name"
class="input-name styled-input"
></v-text-field>
props: {
value: Boolean,
basicInfo: Array
},
My problem is that I can see each key stroke as changes are being made so there is no way to go back to the original if a cancel is selected. I'm new to Vue and Components so I could have something totally screwed up. Why are changes being make to both the basicInfo array and the newbasicInfo array at the same time.
What is happening here is that you are copying the array by reference. So when you modify the index of one array, actually both are modified because they share the same reference.
What you need to do is to copy the array by values.
this can be easily done this way : this.newbasicInfo = JSON.parse(JSON.stringify(this.basicInfo));
You can check this question on SO for more context : How do you clone an Array of Objects in Javascript?
Your clone method does not work with an array of objects:
const basicInfo = [{ firstName: "John" , lastName: "Doe" }]
const newbasicInfo = basicInfo.slice()
This method creates a "shallow copy" - that means it works with numbers, strings, etc. but does not really create a clone of objects. The cloned objects this way keep their reference, so they will actually be the "same" objects as in the source array.
const basicInfo = [{ firstName: "John" , lastName: "Doe" }]
const newbasicInfo = JSON.parse(JSON.stringify(basicInfo))
This method creates a "deep copy" - everything it holds will be cloned, no matter how "deep" they are nested.
If you have an array of simple values, the shallow copy is more efficient, but if you have objects in your array you need deep copy.
So, it's not a Vue problem, but a more general JavaScript question.
Here's a little snippet, to illustrate the difference:
const basicInfo1 = [{
firstName: "John",
lastName: "Doe"
}]
const newbasicInfo1 = basicInfo1.slice()
newbasicInfo1[0].firstName = "Johnnyboy"
console.log('basicInfo1: ', basicInfo1)
console.log('newbasicInfo1: ', newbasicInfo1)
const basicInfo2 = [{
firstName: "John",
lastName: "Doe"
}]
const newbasicInfo2 = JSON.parse(JSON.stringify(basicInfo2))
newbasicInfo2[0].firstName = "Johnnyboy"
console.log('basicInfo2: ', basicInfo2)
console.log('newbasicInfo2: ', newbasicInfo2)

generate custom key with $add angularfire

How can I generate a custom key in array inside angularfire when I add a new record with the $add function. Just look at when the is the comment below in the source code. This is my code below but still get the random key generated by firebase.
register : function(user){
return simpleLogin.$createUser({
email: user.email,
password: user.password
}).then(function(regUser){
//console.dir(regUser);
var ref = new Firebase(FIREBASE_URL + 'users');
var firebaseUsers = $firebaseArray(ref);
var userInfo = {
date: Firebase.ServerValue.TIMESTAMP,
regUser: regUser.uid,
firstname: user.firstname,
lastname: user.lastname,
email: user.email
}
//this is when i want to generate the key
firebaseUsers.$add(userInfo).then(function(ref) {
});
});
},//register
Calling $add will simply always result in a so-called push id. If you don't want to use those push ids to identify the objects, the solution is to not call $add/push.
Instead you can just access the child that you want to create directly:
var ref = new Firebase(FIREBASE_URL + 'users');
var userInfo = {
date: Firebase.ServerValue.TIMESTAMP,
regUser: regUser.uid,
firstname: user.firstname,
lastname: user.lastname,
email: user.email
}
ref.child(regUser.uid).set(userInfo);
Two things to note here:
the snippet creates the child node under regUser.uid. If you need another key, you can substitute that.
this snippet doesn't use AngularFire, but instead uses the Firebase JavaScript SDK directly. Since AngularFire is built on the JavaScript SDK, they interoperate perfectly. So if you need an operation that isn't immediately obvious in AngularFire, it might be worth it to check whether you can accomplish it (more easily) with the regular Firebase JavaScript SDK.
with the help of mester Frank van Puffelen above I modify the code :
register : function(user){
return simpleLogin.$createUser({
email: user.email,
password: user.password
}).then(function(regUser){
//console.dir(regUser);
var ref = new Firebase(FIREBASE_URL + 'users');
var firebaseUsers = $firebaseArray(ref);
var userInfo = {
date: Firebase.ServerValue.TIMESTAMP,
regUser: regUser.uid,
firstname: user.firstname,
lastname: user.lastname,
email: user.email
}
//the added portion of code
ref.child(regUser.uid).set(userInfo);
firebaseUsers.$add(userInfo).then(function(ref) {
});
});
},//register
and i got the result i want , in angularfire API there is no functions ( 'child()' and set() ) but as mentionned in the post of mester Frank van Puffelen angularfire API is built up on firebase API .
and this is my capture image from my database firebase.

Why am I only able to print one object in javascript?

Doing some practice runs on codecademy, and came across the following problem:
I am able to only console.log "Steve Jobs" and all his info, but I want to also
include "Bill Gates." If anyone knows how to do this that would be great, or
any alternatives to the following code:
var friends = {};
friends.bill = {
firstName: "Bill",
lastName: "Gates",
number: "(206)555-5555",
address: ['One Microsoft Way', 'Redmond', 'WA', '98052']
};
friends.steve = {
firstName: "Steve",
lastName: "Jobs",
number: "(206)777-7777",
address: ['Apple Rd.', 'Cupertino', 'CA', '90210']
};
var list = function(list) {
for (var dale in friends) {
console.log(dale);
}
}
var search = function(name) {
for (var key in friends) {
if (name === friends[key].firstName) {
console.log(friends[key]);
return friends[key];
}
}
};
OK, so when I run this code, only Steve gets printed. It should also print Bill.
Created a JSBin from your code with the addition that I called list(); right at the end. If you run the code using CMD+Enter you will see that the output is as expected, printing out both bill and steve
Copy/pasted the code in the console and calling:
search('Bill');
Works just fine for me, how are you using this code?

Convert String containing list of JSON objects to Object Array

A String contains list of Objects which are serialized in JSON format, how to convert that to list of JSON Objects like given in example below preferably without using jQuery.
Eval, Stringyfy Json.parse etc dont seems to help here.
[
{firstName: 'Laurent', lastName: 'Renard', birthDate: new Date('1987-05-21'), balance: 102, email: 'whatever#gmail.com'},
{firstName: 'Blandine', lastName: 'Faivre', birthDate: new Date('1987-04-25'), balance: -2323.22, email: 'oufblandou#gmail.com'},
{firstName: 'Francoise', lastName: 'Frere', birthDate: new Date('1955-08-27'), balance: 42343, email: 'raymondef#gmail.com'}
];
Update:- JSON String
[
{"attributes":{"type":"Contact","url":"/services/data/v30.0/sobjects/Contact/0039000000wvt6yAAA"},"Name":"Stella Pavlova","Phone":"(212) 842-5500","CreatedDate":"2014-05-15T06:17:48.000+0000","Id":"0039000000wvt6yAAA"},
{"attributes":{"type":"Contact","url":"/services/data/v30.0/sobjects/Contact/0039000000wvt6zAAA"},"Name":"Lauren Boyle","Phone":"(212) 842-5500","CreatedDate":"2014-05-15T06:17:48.000+0000","Id":"0039000000wvt6zAAA"},
{"attributes":{"type":"Contact","url":"/services/data/v30.0/sobjects/Contact/0039000000wvt70AAA"},"Name":"Babara Levy","Phone":"(503) 421-7800","CreatedDate":"2014-05-15T06:17:48.000+0000","Id":"0039000000wvt70AAA"},
{"attributes":{"type":"Contact","url":"/services/data/v30.0/sobjects/Contact/0039000000wvt71AAA"},"Name":"Josh Davis","Phone":"(503) 421-7800","CreatedDate":"2014-05-15T06:17:48.000+0000","Id":"0039000000wvt71AAA"},
{"attributes":{"type":"Contact","url":"/services/data/v30.0/sobjects/Contact/0039000000wvt72AAA"},"Name":"Jane Grey","Phone":"(520) 773-9050","CreatedDate":"2014-05-15T06:17:48.000+0000","Id":"0039000000wvt72AAA"},
{"attributes":{"type":"Contact","url":"/services/data/v30.0/sobjects/Contact/0039000000wvt73AAA"},"Name":"Arthur Song","Phone":"(212) 842-5500","CreatedDate":"2014-05-15T06:17:48.000+0000","Id":"0039000000wvt73AAA"},
{"attributes":{"type":"Contact","url":"/services/data/v30.0/sobjects/Contact/0039000000wvt74AAA"},"Name":"Ashley James","Phone":"+44 191 4956203","CreatedDate":"2014-05-15T06:17:48.000+0000","Id":"0039000000wvt74AAA"},
{"attributes":{"type":"Contact","url":"/services/data/v30.0/sobjects/Contact/0039000000wvt75AAA"},"Name":"Tom Ripley","Phone":"(650) 450-8810","CreatedDate":"2014-05-15T06:17:48.000+0000","Id":"0039000000wvt75AAA"},
{"attributes":{"type":"Contact","url":"/services/data/v30.0/sobjects/Contact/0039000000wvt76AAA"},"Name":"Liz D'Cruz","Phone":"(650) 450-8810","CreatedDate":"2014-05-15T06:17:48.000+0000","Id":"0039000000wvt76AAA"},
{"attributes":{"type":"Contact","url":"/services/data/v30.0/sobjects/Contact/0039000000wvt77AAA"},"Name":"Edna Frank","Phone":"(650) 867-3450","CreatedDate":"2014-05-15T06:17:48.000+0000","Id":"0039000000wvt77AAA"},
{"attributes":{"type":"Contact","url":"/services/data/v30.0/sobjects/Contact/0039000000wvt78AAA"},"Name":"Avi Green","Phone":"(212) 842-5500","CreatedDate":"2014-05-15T06:17:48.000+0000","Id":"0039000000wvt78AAA"},
{"attributes":{"type":"Contact","url":"/services/data/v30.0/sobjects/Contact/0039000000wvt79AAA"},"Name":"Siddartha Nedaerk","CreatedDate":"2014-05-15T06:17:48.000+0000","Id":"0039000000wvt79AAA"},
{"attributes":{"type":"Contact","url":"/services/data/v30.0/sobjects/Contact/0039000000wvt7AAAQ"},"Name":"Jake Llorrac","CreatedDate":"2014-05-15T06:17:48.000+0000","Id":"0039000000wvt7AAAQ"},
{"attributes":{"type":"Contact","url":"/services/data/v30.0/sobjects/Contact/0039000000wvt6rAAA"},"Name":"Rose Gonzalez","Phone":"(512) 757-6000","CreatedDate":"2014-05-15T06:17:48.000+0000","Id":"0039000000wvt6rAAA"},
{"attributes":{"type":"Contact","url":"/services/data/v30.0/sobjects/Contact/0039000000wvt6sAAA"},"Name":"Sean Forbes","Phone":"(512) 757-6000","CreatedDate":"2014-05-15T06:17:48.000+0000","Id":"0039000000wvt6sAAA"},
{"attributes":{"type":"Contact","url":"/services/data/v30.0/sobjects/Contact/0039000000wvt6tAAA"},"Name":"Jack Rogers","Phone":"(336) 222-7000","CreatedDate":"2014-05-15T06:17:48.000+0000","Id":"0039000000wvt6tAAA"},
{"attributes":{"type":"Contact","url":"/services/data/v30.0/sobjects/Contact/0039000000wvt6uAAA"},"Name":"Pat Stumuller","Phone":"(014) 427-4427","CreatedDate":"2014-05-15T06:17:48.000+0000","Id":"0039000000wvt6uAAA"},
{"attributes":{"type":"Contact","url":"/services/data/v30.0/sobjects/Contact/0039000000wvt6vAAA"},"Name":"Andy Young","Phone":"(785) 241-6200","CreatedDate":"2014-05-15T06:17:48.000+0000","Id":"0039000000wvt6vAAA"},
{"attributes":{"type":"Contact","url":"/services/data/v30.0/sobjects/Contact/0039000000wvt6wAAA"},"Name":"Tim Barr","Phone":"(312) 596-1000","CreatedDate":"2014-05-15T06:17:48.000+0000","Id":"0039000000wvt6wAAA"},
{"attributes":{"type":"Contact","url":"/services/data/v30.0/sobjects/Contact/0039000000wvt6xAAA"},"Name":"John Bond","Phone":"(312) 596-1000","CreatedDate":"2014-05-15T06:17:48.000+0000","Id":"0039000000wvt6xAAA"}
]
I edited your post to make it more visibly clear, so maybe now you'll see: The value you posted as your "JSON String" is actually a JavaScript array of objects; exactly what you're claiming you're trying to convert the string into. You should read up more on JSON so you fully understand how you can convert to and from JSON, but that's already a valid JavaScript object -- you don't need to do anything to it.
Here's a JSFiddle showing this fact in action -- all you need to do is set a variable equal to the block of code you're receiving, and you can access it as a normal array/object. http://jsfiddle.net/ZpXVh/
var array = /* your really long code */;
alert(array[1]['name'];

Categories