Nick7420609
Posts: 0
Joined: Sun Mar 08, 2015 6:00 pm

same code - JS vs Apperyio - different results

Very confused, hope someone can help to understand why I'm getting different results when running in JS vs Apperyio model?

As you can see on the results, when using Apperyio approach, it saves 4 times but always has the last ( page4) page for some reason.

Model :

Image

Storage:

Image

Code :

function check_service_selections(){

Code: Select all

  var service_selections = ['page1', 'page2', 'page3', 'page4']; 

     var service_list_obj = {};  // JS object 
     var ServiceList = Apperyio.getModel('ServiceList'); // Appery model  

     $.each(service_selections, function(key, value){ 

         var service_obj = {};   
         var Service = Apperyio.getModel('Service'); 

         Service.page = value; 
         Service.status = 0; 

         service_obj.page = value; 
         service_obj.status = 0; 

         service_list_obj[key] = service_obj; 
         ServiceList[key] = Service; 

     }); 

     // save in storage 
     Apperyio.storage.service_list.set(service_list_obj); 

     // read from storage 
     console.log(Apperyio.storage.service_list.get());  // this prints an array of objects, which is what I expect 

  // This part of the code doesn't work as it should. All objects have the same ( last ) page name, in this case, page4 
     // save in storage 
     Apperyio.storage.service_list.set(ServiceList); 

     // read from storage 
     console.log(Apperyio.storage.service_list.get()); // this also returns an array of objects, but every object has the same "page" name 
 } 

Results :

Image

Thanks!

Egor Kotov6832188
Posts: 0
Joined: Wed Nov 19, 2014 5:15 pm

same code - JS vs Apperyio - different results

Hello Nick,

Apparently, you have wrong mapping.
Could you post here screenshot how you are filling your LSV?

Nick7420609
Posts: 0
Joined: Sun Mar 08, 2015 6:00 pm

same code - JS vs Apperyio - different results

Hi Egor,

What is LSV?

I have a static data in the array "var service_selections = ['page1', 'page2', 'page3', 'page4'];

not sure what the mapping has to do, when using static data

Nick7420609
Posts: 0
Joined: Sun Mar 08, 2015 6:00 pm

same code - JS vs Apperyio - different results

Egor, also I'd like to mention that the application is shared with support, in case you need to reproduce to see the results.

thanks

Egor Kotov6832188
Posts: 0
Joined: Wed Nov 19, 2014 5:15 pm

same code - JS vs Apperyio - different results

Nick,

LSV - local storage variable

Try to save all in variable like here yo udid already:
var service_selections = ['page1', 'page2', 'page3', 'page4'];

then save it variable created in Model&Storage
Apperyio.storage.MY_VAR.set(service_selections);

Nick7420609
Posts: 0
Joined: Sun Mar 08, 2015 6:00 pm

same code - JS vs Apperyio - different results

Egor,

I think you misunderstood my goal, so I'll explain in more details :

The service needs to be a model with 2 properties :

Service:
page: page1
status: 0

That's why I have a such a model.

So what I'm trying to do is, the following :

  1. Iterate through a list of pages ( service_selections = ['page1', 'page2', 'page3', 'page4']; )

  2. create an instance for each page of a Page model that I created in Appery models

  3. set the page=page1, status=0 for each page

  4. save each instance in a local storage variable in an array, so the end result should be an array that consists of Service models

    If you try to run the code, you will see, that when using plain JS everything works, so the issue appears to be somewhere around here :

    // when creating an instance of the model

    var Service = Apperyio.getModel('Service');

    // or when saving to local storage variable, I don't understand why I'm ending up with the latest page ( page4) in the array, when using the Apperyio's recommended approach.

    Please try to run the snippet and you will see the issue.

    As for saving per your suggestion :

    Apperyio.storage.MY_VAR.set(service_selections);

    I will end up with the following :

    ['page1', 'page2', 'page3', 'page4']

    however I need to end up with an array of objects ( image provided above)

    Thanks

Yurii Orishchuk
Posts: 0
Joined: Fri Feb 14, 2014 8:20 am

same code - JS vs Apperyio - different results

Hi Nick,

"getModel" function returns the same object every time you invoke thus you modified the same object.

Here is a correct JS code for your goal:

pre

var service_selections = ['page1', 'page2', 'page3', 'page4'];

var service_list_obj = {}; // JS object
var ServiceList = Apperyio.getModel('ServiceList'); // Appery model

$.each(service_selections, function(key, value) {

Code: Select all

 var service_obj = {}; 
 var Service = jQuery.extend(true, {}, Apperyio.getModel('Service')); 

 Service.page = value; 
 Service.status = 0; 

 service_obj.page = value; 
 service_obj.status = 0; 

 service_list_obj[key] = service_obj; 
 ServiceList[key] = Service; 

});

// save in storage
Apperyio.storage.service_list.set(service_list_obj);

/pre

Regards.

Nick7420609
Posts: 0
Joined: Sun Mar 08, 2015 6:00 pm

same code - JS vs Apperyio - different results

Hi Yurii,

Thank you very much, this worked like a charm!

Return to “Issues”