viewModel null inside BroadCastReceive for the first time

nativescriptcore

#1

Hello Everyone I am facing strange problem with my nativescript app which installed in sonim deveice (http://www.sonimtech.com/index.php/products/device/device/SonimXP7_3)
when I installed App for then first time I am getting dockReceive.get(“scanType”) = null in broadcast receive. though I am setting it to load event of home page.
When I clear shared preference or install it for the first time. My viewModel not allow me to set or get it’s
(dockReceive’s) properties in onReceiveCallback(context, intent) function.


viewModel.js

       var observableModule = require("data/observable");
 var observableArrayModule = require("data/observable-array");

function DockReceive(info) {
info = info || {};
console.log("in dock receive");
// You can add properties to observables on creation
 var viewModel = new observableModule.fromObject({

uname:info.uname || "",
packingSlip: info.packingSlip || "",
scanType: info.scanType || "",
});

return viewModel;
}
module.exports = DockReceive;

app.js

     var applicationModule = require("application");
var context = android.content.Context;
var intent = android.content.Intent;
var platform = require("platform");
var dialogsModule = require("ui/dialogs");
var observableArrayModule = require("data/observable-array");
var appSettings = require("application-settings");
var DockReceiveViewModel = require("./shared/view-models/dock-receive-view-model");
var dockReceive = new DockReceiveViewModel();
if (applicationModule.android) {
 applicationModule.android.registerBroadcastReceiver(
"com.sonim.barcode_read",    ---------------------> (this call when I press yellow hardware button to scan barcode)
function onReceiveCallback(context, intent) {

  if (intent != null) {
    var data = intent.getStringExtra("data"); -------------------> this is working every time.
    console.log("data : : :" + intent.getStringExtra("data"));
    console.log("intent:"+intent.getAction());
    if (data != null && data.length > 0) {
      if (dockReceive !== null) {
       
        var user = appSettings.getString("uname");
        var type = dockReceive.get("scanType");
 // when I run or install app for the first time the type will be null(blank); so executes default case
// if I restart the app means re run using console then it will work perfectly until i logout from then app.  

        console.log("Scan type is:" + type);

        switch (type) {
          case "ScanPackingSlip":
            dockReceive.set("packingSlip", data);
            break;
 default:
            dialogsModule.alert("Unknown Scan Type.");
            break;
     
  }
}
 );
}

if (appSettings.getBoolean("logedIn") == undefined) {
//Starting point of app
applicationModule.start({
 moduleName: "views/login/login",
 context: dockReceive
});
} else {
 applicationModule.start({
  moduleName: "views/home/home",
context: dockReceive
});
}

dockreceive.xml

    <Page xmlns="http://schemas.nativescript.org/tns.xsd" loaded="pageLoaded" navigatedFrom="navigateFrom" xmlns:dd="nativescript-drop-down" class="main">
<ActionBar title="Dock Receive" class="menu" id="actionbar">
<NavigationButton text="Go Back" android.systemIcon="ic_menu_back" tap="onNavBtnTap"/>
<ActionItem  text="Delete Batch" id="bdelete" android.position="popup" visibility="hidden" tap="onBatchDelete" />
</ActionBar>

<ScrollView>
 <StackLayout class="main">
 <TextField  text="{{ packingSlip }}" hint="Scan Packing Slip" id="pslip"  keyboardType="number" ios.keyboardType="UIKeyboardTypeNumbersAndPunctuation" class="dropdownlist"></TextField>
</StackLayout>
</ScrollView>
</Page>

dockreceive.js

    exports.pageLoaded = function(args) {
    var page = args.object;
      page.bindingContext = page.navigationContext;
      dockReceive = page.navigationContext;
      dockReceive.set("scanType","ScanPackingSlip");
  }

#2

@multishiv19 hi, could you able to help me with this?


#3

I’m not familiar with this kind of device; however, you seem to be trying to get a value that is not fully initialized and ready yet in the first run, so try to wrap it around some timeout that might help. Also If you have more complicated logic like this that takes some time to initialize, then you better put it in a router page instead of app.js, it will be less error prone


#4

Whenever you use get methods of appSettings, always remember to pass the default parameter as the second argument

Example
appSettings.getString(“user”, “{}”};
appSettings.getBoolean(“loggedIn”, false);

That way you can check if it was initialized or not, easily.
And have a default value if it wasn’t initialized…


#5

thank you everyone.

I am able to find somewhat solution to my problem by checking brocastreceiver in each page where I am scanning barcode using that yellow button, instead of calling it in app.js.