0

Sorry for this noobie question, but I've been searching all the existing answer about shadowing variable, but still can't make my code work

export default Ember.ArrayController.extend({
 actions: {
 generateInvoice: function() {
 var amount1; // declare a variable
 var amount2;
 this.store.find('product', 1).then(function(obj){
 amount1 = 100; //assign value to the amount variable
 });
 this.store.find('product', 2).then(function(obj){
 amount2 = 200; 
 });
 console.log(amount1); // undefined!
 console.log(amount2); // undefined!
 $.post('http://nzud.co.nz/invoice',{
 foo1: amount1, //access variable amount ---doesn't work!!
 foo2: amount2
 } ).done(function(data){
 alert("success!");
 })
 }
 }
});

I've tried a lot of way, but still can't store the model record property into a variable and access from the $.post payload

asked Jul 31, 2014 at 13:11
2
  • this.store.find is asynchronous Commented Jul 31, 2014 at 13:14
  • @adeneo thanks for pointing it out. I need to learn more about promise Commented Jul 31, 2014 at 13:22

2 Answers 2

2

To proceed using the variable new value, you have to put your code inside the promisse then. Then it will work:

export default Ember.ArrayController.extend({
 actions: {
 generateInvoice: function() {
 this.store.find('product', 1).then(function(obj){
 var amount = 100; //assign value to the amount variable
 $.post('http://nzud.co.nz/invoice',{
 foo: amount
 }).done(function(data){
 alert("success!");
 })
 });
 }
 }
});

This happens because the method find is async, so you have to work with its result inside a callback function, in this case the function then. Everything inside generateInvoice scope will (probably) be called before your find method request ends, even if its the first method called inside it.

UPDATE:

I didn't get what you mean with those two find methods for product, 1, but:

  • If you have two values in the request to send to the post, you should use the find() method only once:

    this.store.find('product', 1).then(function(obj){
     var amount = 100; //assign value to the amount variable
     $.post('http://nzud.co.nz/invoice',{
     foo: amount,
     bar: another_value
     })
    });
    
  • Or if you have to call two differents find methods, you have to chain them:

    this.store.find('product', 1).then(function(obj){
     var amount = 100; //assign value to the amount variable
     this.store.find('product', 1).then(function(obj){
     var another_amount = 100; //assign value to the amount variable
     $.post('http://nzud.co.nz/invoice',{
     foo: amount,
     bar: another_amount
     })
     });
    });
    
answered Jul 31, 2014 at 13:13
Sign up to request clarification or add additional context in comments.

7 Comments

thanks for your quick answer. I just update my question, there are actually two record query promise, so I can't put the $.post into one of these
@mko You have to post both values once in the post request?
Yes, the payload in the $.post contains two value from two asyc record query
Sorry for the typo, the second is store.find('product',2)
@mko In this case, yes. You have no another way to do it. So you start a new request after another finishes and so on...
|
-1

Declare amount outside of your function:

var amount; // declare a variable
export default Ember.ArrayController.extend({
 actions: {
 generateInvoice: function() {
 this.store.find('product', 1).then(function(obj){
 amount = 100; //assign value to the amount variable
 });
 console.log(amount) // undefined!
 $.post('http://nzud.co.nz/invoice',{
 foo: amount, //access variable amount ---doesn't work!!
 } ).done(function(data){
 alert("success!");
 })
 }
 }
});
answered Jul 31, 2014 at 13:13

3 Comments

This maybe call the post function with the amount undefined.
Not sure what you mean there, but yeah I missed the part of this being async. Amount still doesn't have a value by the time the post function is ran.
This is exactly what I mean.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.