Unable to push data to viewModel


#1

ClientModel.js

var fetchModule = require("fetch");
var http = require("http");
var observableModule = require("data/observable");
var config = require("../../shared/config");
var observableArrayModule = require("data/observable-array").ObservableArray;
function Clients(info) {

 console.log("working")

 var viewModel = new observableArrayModule(info);
viewModel.Id = new observableArrayModule();
viewModel.Description = new observableArrayModule();

viewModel.load = function()
{
var counter = 0
console.log("In get cl");
return fetch(config.clientsApiUrl)
.then(handleErrors)
.then(function(response) {
    console.log("In get clien");
    return response.json();
}).then(function(data) {
    console.log("In get cls:" + JSON.stringify(data));
    for(var i=0;i<=data.length;i++)
    {
        //console.log("length: " + data[i].ID);
        counter++;

        viewModel.push({
            Id: data[i].ID,
            Description: data[i].Description
        });
        console.log("counter length: "+ counter);
    }

}); 

};
   viewModel.empty = function() {
while (viewModel.length) {
    viewModel.pop();
}
 };
 viewModel.addEventListener(observableModule.Observable.propertyChangeEvent, function (pcd) {
console.log(pcd.eventName.toString() + " " + pcd.propertyName.toString() + " " + pcd.value.toString());
  });
return viewModel;

}

 function handleErrors(response) {
if (!response.ok) {
console.log(JSON.stringify(response));
throw Error(response.statusText);
}
return response;
 }

 module.exports = Clients;

Controller - abc.js

 var ClientModel = require("../../shared/view-models/client-models");
 var clients = new ClientModel();
  exports.loaded = function(args) {
var page = args.object;
dockReceive = new DockReceiveViewModel();
page.bindingContext = page.navigationContext;
clients.load();
clients= page.navigationContext;

printNew = page.navigationContext;
if(page.navigationContext==null)
{
console.log("home load null");
}
};

XML Binding

<StackLayout horizontalAlignment="stretch" class="dropdownlist">
 <dd:DropDown id="pdrop" items="{{ Description }}" title="Select Parent Account" hint="Select Parent Account" color="black"
         SelectionChangeCommitted="dropDownParentSelectedIndexChanged" selectedIndex="{{ selectedParentAccountIndex }}" opened="dropDownOpened" closed="dropDownClosed" 
         row="0" colSpan="2" class="drop"/>
         </StackLayout>

I am not able to print anything when calling clients.load(); I am getting json response as I wanted but convert it to viewmodel format and bind it to dropdown of my xml. I think I am not able to push data to viewModel.please let me know if anyone found anything wrong with it.


#2

Hi @radhe12,

I’m not sure I understand what you’re trying to accomplish. Why are you doing this?

clients.load();
clients= page.navigationContext;

#3

@radhe12
Like @neddinn said,
we’re not able understand what you are trying to use.
Please read up on view models in the nativescript docs.
It doesn’t look like you’re using them as intended.


#4

thank you for taking in consideration but I found my solution. by inserting only this way in viewModel

         for(var i=0;i<=data.length;i++)
        {
            //console.log("length: " + data[i].ID);
          viewModel.get("Id").push(data[i].ID);
          viewModel.get("Description").push(data[i].Description);
      //    console.log("viewmodel length: "+ viewModel.length);
        }

#5

@radhe12 try to call/ initialize the observable array like this outside of your function right at the top

var fetchModule = require("fetch");
var http = require("http");
var config = require("../../shared/config");
var ObservableArray = require ("data/observable-array");
var  view = require ("data/observable-array");
var viewModel=new view.Observable();
viewModel.Id=new ObservableArray.ObservableArray([]);
viewModel.Description=new ObservableArray.ObservableArray([]);


then in your viewModel.load function try to push like this

var id=data[i].ID;
var description=data[i].Description;
viewModel.id.push(id);
viewModel.Description.push(description);
console.log("Descriptions :"  JSON.stringify(ViewModel.Description));

#6

For me, it is other way around. viewModel.push is working, not with viewModel.get(“Id”).push.

Any idea?

Thank you,
Seyed Ismail.


#7

Can you show us some code what exactly you are trying to do?