2017-01-27 6 views
5

Budujemy aplikacji przy użyciu Cordova i kątowe 2. Mam następujący kod:kątowa 2 Strefa wykrywania zmian i są różne w 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); 
       }); 
      } 
    } 

w przeglądarce, to działa świetnie. Po uruchomieniu funkcji jsonStoreInit() ustawia ona stan o statusie i aktualizuje interfejs użytkownika do "Inicjowanie JSONStore". W aplikacji Cordova, jeśli nie korzystam z ręcznego wykrywania zmian, nie zaktualizuję interfejsu użytkownika. Na przykład, patrz poniżej gdzie mam // JEŚLI TO NIE JEST naszym NIE Update w Cordova:

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

Jestem również obejrzeniu tego na prostych kliknięć przycisku, aby ustawić zmienną. W Cordova nic się nie dzieje, chyba że ręcznie skorzystam z funkcji wykrywania zmian. Uczę się właśnie Angulara 2, więc wszelka pomoc w tym, co robię źle, jest bardzo doceniana.

Odpowiedz

3

zone.js łaty XHR obiektów i innych API jak setInterval, addEventListener, Obietnica tak kątowa jest powiadamiany, gdy coś się dzieje, a to wyzwala samo wykrycie zmian.

Wygląda JSONStore wykorzystuje inną implementację Obietnica (jQuery?), Który nie jest załatana przez zone.js, więc trzeba ręcznie wywołać wykrywanie zmian lub owinąć Ci zwrotnych w zone.run.

+1

Podążam za tym, co mówisz. Nadal nie rozumiem, dlaczego będzie działać, gdy uruchomi się ten sam kod bez ręcznego wykrywania zmian na serwerze węzła. Następnie, gdy zostanie wdrożony na urządzeniu mobilnym w opakowaniu Cordova, nie działa. –

+0

Spróbuj dodać 'console.log ('zone', Zone.current);', aby sprawdzić bieżącą strefę. Strefa jest zmienną globalną, może będziesz musiał dodać definicję pisania, aby z niej skorzystać. – kemsky

+0

Przepraszamy za opóźnienie .... To jest rejestrowane jako "kątowe" w Internecie, a rodzicem jest strefa. Kiedy sprawdzam konsolę z cordova, jest ona rejestrowana jako "", a rodzic ma wartość zerową. –

Powiązane problemy