2017-01-27 16 views
5

Biz Aşağıdaki kod var bir Cordova kullanarak uygulamayı ve Açısal 2. inşa ediyoruz Farklı şunlardır: Bir web tarayıcısındaEğik 2 Değişim Algılama ve Bölge Cordova App

import { Component, OnInit, ChangeDetectorRef, NgZone } from '@angular/core'; 
    import { Location } from '@angular/common'; 

    declare var WL : any; 

    @Component({ 
     selector: 'app-store', 
     templateUrl: './store.component.html', 
     styleUrls: ['./store.component.css'] 
    }) 

    export class StoreComponent implements OnInit { 
     status: string; 
     document: any; 

     constructor(private _location: Location, private changeDetector: ChangeDetectorRef, 
     private zone: NgZone) { } 

     ngOnInit() { 
     var collectionName = 'people'; 
     this.status = "JSONStore is not yet initialized!"; 
     if(typeof WL !== "undefined" && typeof WL.JSONStore.get(collectionName) !== "undefined") 
       this.status = "JSONStore is initialized!"; 
     } 
     } 
     jsonStoreInit(){ 
       var that = this; 
       var collectionName = 'people'; 

       // Object that defines all the collections. 
       var collections = { 

        // Object that defines the 'people' collection. 
        people : { 

         // Object that defines the Search Fields for the 'people' collection. 
         searchFields : {name: 'string', age: 'integer'} 
        } 
       }; 

       // Optional options object. 
       var options = { }; 

       /* // Optional username, default 'jsonstore'. 
       username : 'carlos', 

       // Optional password, default no password. 
       password : '123', 

       // Optional local key generation flag, default false. 
       localKeyGen : false 
       };*/ 

       WL.JSONStore.init(collections, options).then(function() { 

        // Data to add, you probably want to get 
        // this data from a network call (e.g. MobileFirst Adapter). 
        var data = [{name: 'carlos', age: 10}]; 

        // Optional options for add. 
        var addOptions = { 

         // Mark data as dirty (true = yes, false = no), default true. 
         markDirty: true 
        }; 

        // Get an accessor to the people collection and add data. 
        return WL.JSONStore.get(collectionName).add(data, addOptions); 
       }) 

       .then(function (numberOfDocumentsAdded) { 
        that.status = "JSONStore is initialized!"; 
       }) 

       .fail(function (errorObject) { 
       // Handle failure for any of the previous JSONStore operations (init, add). 
        alert("Error"); 
        console.log(errorObject); 
       }); 
      } 
    } 

, bu harika çalışıyor. JsonStoreInit() işlevi tetiklendiğinde, durumu durumunu ayarlar ve UI'yi "JSONStore başlatıldı" olarak güncelleştirir. Cordova uygulamasında, manuel değişiklik tespitini kullanmazsam, kullanıcı arayüzünü güncellemez. Örneğin, BU BURADA DEĞİL İSE Cordova İÇİNDE GÜNCELLENECEK OLMAYACAKTIR // Ben sahip olduğu aşağıya bakınız:

 ngOnInit() { 
      var collectionName = 'people'; 
      this.status = "JSONStore is not yet initialized!"; 
      if(typeof WL !== "undefined" && typeof WL.JSONStore.get(collectionName) !== "undefined") 
       this.status = "JSONStore is initialized!"; 

       //IF THIS ISN'T HERE, IT WILL NOT UPDATE IN CORDOVA 
       this.changeDetector.markForCheck(); 
       this.zone.run(()=> function(){}); 
      } 
     } 

     jsonStoreInit(){ 
      var that = this; 
      var collectionName = 'people'; 

      // Object that defines all the collections. 
      var collections = { 

       // Object that defines the 'people' collection. 
       people : { 

        // Object that defines the Search Fields for the 'people' collection. 
        searchFields : {name: 'string', age: 'integer'} 
       } 
      }; 

      // Optional options object. 
      var options = { }; 

      /* // Optional username, default 'jsonstore'. 
      username : 'carlos', 

      // Optional password, default no password. 
      password : '123', 

      // Optional local key generation flag, default false. 
      localKeyGen : false 
      };*/ 

      WL.JSONStore.init(collections, options).then(function() { 

       // Data to add, you probably want to get 
       // this data from a network call (e.g. MobileFirst Adapter). 
       var data = [{name: 'carlos', age: 10}]; 

       // Optional options for add. 
       var addOptions = { 

        // Mark data as dirty (true = yes, false = no), default true. 
        markDirty: true 
       }; 

       // Get an accessor to the people collection and add data. 
       return WL.JSONStore.get(collectionName).add(data, addOptions); 
      }) 

      .then(function (numberOfDocumentsAdded) { 
       that.status = "JSONStore is initialized!" 

       //IF THIS ISN'T HERE, IT WILL NOT UPDATE IN CORDOVA 
       this.changeDetector.markForCheck(); 
       this.zone.run(()=> function(){}); 
      }) 

      .fail(function (errorObject) { 
      // Handle failure for any of the previous JSONStore operations (init, add). 
       alert("Error"); 
       console.log(errorObject); 
      }); 
     } 

Ben de bir değişken ayarlamak için basit düğme tıklama bu görüyorum. Değişiklik tespitini elle kullanmadığım sürece Cordova'da hiçbir şey olmaz. Ben sadece Angular 2'yi öğreniyorum, bu yüzden yanlış yaptığım herhangi bir yardım büyük beğeni topluyor. bir şey olduğunda

cevap

3

zone.js yamaları setInterval gibi XHR nesne ve diğer apis, addEventListener Promise yüzden açısal bildirilir ve değişim algılama kendisi tetikler.

JSONStore farklı Promise uygulamasını kullanıyor gibi görünüyor (jQuery?) El değiştirme algılama tetikleyebilir ya zone.run size geri aramalar sarmak zorunda, zone.js tarafından yamalı değildir.

+1

Ne dediğinizi takip ediyorum. Hala bir düğüm sunucusunda manüel değişiklik tespiti olmadan aynı kodu çalıştırırken neden çalışacağı konusunda kafam karıştı. Daha sonra Cordova sarıcısı içinde bir mobil cihaza dağıtıldığında, işe yaramıyor. –

+0

Geçerli bölgeyi kontrol etmek için 'console.log ('zone', Zone.current); Bölge global değişkendir, kullanmak için yazım tanımını eklemeniz gerekebilir. – kemsky

+0

Gecikme için özür dilerim ... Web'de "açısal" olarak kaydediliyor ve ebeveyn Bölge. Konsolu cordova'dan kontrol ettiğimde, "" olarak kaydedilir ve üst öğe boştur. –