Unable to push data to viewModel



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) {


 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(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);

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


   viewModel.empty = function() {
while (viewModel.length) {
 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) {
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= page.navigationContext;

printNew = page.navigationContext;
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"/>

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.


Hi @radhe12,

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

clients= page.navigationContext;


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.


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);
      //    console.log("viewmodel length: "+ viewModel.length);


@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;
console.log("Descriptions :"  JSON.stringify(ViewModel.Description));


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

Any idea?

Thank you,
Seyed Ismail.


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