PDA

View Full Version : How to pass values through function



celsospina
16 Mar 2015, 4:05 AM
Dear friends, I am right new in Extjs 4.

I have this store which has only one record:


var storePermissao = Ext.create('Ext.data.Store', {
fields: ["codigo", "pbancos"],
autoLoad: true,
//autodestroy: true,
proxy: {
type: "ajax",
url: "permissoes.php",
reader: {
type: "json",
root: "data"
}
}
});

And I want to get the value from one field called bancos.
Using the code below I can access the value, but I can use in my application


var permissaoUsuario =
storePermissao.load({
scope: this,
callback: function(records, operation, success){
if(success){
var req = storePermissao.first();
var teste = req.get('pbancos');
console.log('teste');
}
}
});
Is there a way to pass the value from teste to permissaoUsuario?

I have tried several maners, but none results.

Any ideas?
Thans very much!

yeghikyan
16 Mar 2015, 4:46 AM
you can use store's load event to do it.

celsospina
16 Mar 2015, 9:23 AM
Right, but how? What and where I have to put the lines?

skirtle
16 Mar 2015, 1:29 PM
If you just have a single record you can load it directly via as a model rather than using a store. However, you'll still have the same problem.

You can assign it like this:


var permissaoUsuario;

storePermissao.load({
callback: function(records, operation, success) {
if (success) {
var req = storePermissao.first();
permissaoUsuario = req.get('pbancos');
}
}
});

This is just a closure over the variable. It'll assign the value but unless it's captured in other closures that won't achieve much. Keep in mind that the callback fires asynchronously, so the value won't be assigned until after the request has returned from the server. It won't be assigned in the code immediately following the call to load.

If you want something to happen with permissaoUsuario once it's assigned then you'll need to invoke a suitable function/method from within the callback, just assigning the value won't be enough.

celsospina
16 Mar 2015, 3:36 PM
Dear skirtle, Thank for your answer. I have done your suggestion, but it didn't work. I have put a alert after, and the message was object Object. May be you can help me, I just need to ready a value from a store. I am trying everything. The value for pbancos is 4, I need just to read that. You can forget callback, everything. Thanks in advance.

skirtle
16 Mar 2015, 5:13 PM
I have put a alert after, and the message was object Object.

I'd avoid using alert for debugging if I were you. Use console.log instead whenever possible. If you use console.log (or console.dir) you'll be able to inspect the object to find out exactly which object it is (assuming you're using a recent browser for debugging). That may help to explain what isn't working.

Unless you post your exact code there's not a lot more I can suggest.


You can forget callback, everything.

You need to use a callback function of some kind, that can't be helped. Whether you use a direct callback or a load listener it will still come down to the same thing. The callback is asynchronous and you have to wait for it to be called. If you need the value immediately (synchronously) then you're going to have to redesign to ensure it's loaded before you get this far.

The use of callbacks for AJAX requests is standard for all JavaScript libraries and all the same rules and techniques apply here. While parts of ExtJS can closely resemble magic, try not to forget that it's all just JavaScript.

celsospina
17 Mar 2015, 4:37 AM
Thanks for the tip, but it stillnothing workig, please see the code:

var permissaoUsuario =
storePermissao.load({
scope: this,
callback: function(records, operation, success){
if(success){
var req = storePermissao.first();
permissaoUsuario = req.get([0]).get('pbancos');
}
}
});


console answer: constructor {groupers: constructor, groups: constructor, autoLoad: true, proxy: constructor, removed: Array[0]}

celsospina
17 Mar 2015, 6:21 AM
Dear skirtle, thank you again. You make me give one more step. Tha store is not populate when I run the fucntion at the first time (Ext.onReady(function(). That's why it was giving the error. When I change the for the store populte. Thanks again. How to force populate the store?

skirtle
17 Mar 2015, 9:05 AM
Your new-lines are deceptive. What you have is this:


var permissaoUsuario = storePermissao.load({...});

The return value from load is what you're seeing in the console logging. Note that my version didn't include the =.

If you need the store to be loaded before this code runs then you'll need to ensure the user interactions that trigger this code aren't available. If necessary you may have to delay the showing of the entire UI until after the store is loaded.