2017-01-27 25 views
5

हम मैं निम्नलिखित कोड है एक आवेदन Cordova का उपयोग करने और कोणीय 2. निर्माण कर रहे हैं में अलग हैं:कोणीय 2 पहचान और क्षेत्र बदलें Cordova अनुप्रयोग

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

एक वेब ब्राउज़र पर, इस महान काम करता है। जब jsonStoreInit() आग लगती है, तो यह स्थिति सेट करती है और "JSONStore प्रारंभ में" UI को अपडेट करती है। कॉर्डोवा ऐप पर, यदि मैं मैन्युअल परिवर्तन पहचान का उपयोग नहीं करता, तो यह यूआई अपडेट नहीं करेगा। मैं भी एक चर सेट करने के लिए सरल बटन क्लिक पर यह देख रहा हूँ

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

: उदाहरण के लिए, // यदि यह यहाँ नहीं है, यह CORDOVA में अपडेट नहीं होगा, जहां मैं है नीचे देखें। कॉर्डोवा में कुछ भी नहीं होता है जब तक कि मैं मैन्युअल रूप से परिवर्तन पहचान का उपयोग नहीं करता। मैं केवल कोणीय 2 सीख रहा हूं, इसलिए जो भी मैं गलत कर रहा हूं उस पर कोई मदद की सराहना की जाती है।

उत्तर

3

zone.js पैच XHR वस्तु और setInterval जैसे अन्य APIs, addEventListener, वादा तो कोणीय सूचित किया जाता है जब कुछ होता है और यह परिवर्तन का पता लगाने में ही चलाता है।

ऐसा लगता है कि JSONStore की तरह अलग अलग वादा कार्यान्वयन उपयोग कर रहा है (jQuery?) जो zone.js से समझौता नहीं कर रहा है, तो आप मैन्युअल रूप से परिवर्तन का पता लगाने को गति प्रदान या zone.run में आप कॉलबैक रैप करने के लिए किया है।

+1

मैं जो कह रहा हूं उसका पालन करता हूं। मैं अभी भी उलझन में हूं कि यह नोड सर्वर पर मैन्युअल परिवर्तन पहचान के बिना एक ही कोड चलाते समय क्यों काम करेगा। फिर जब इसे कॉर्डोवा रैपर के अंदर एक मोबाइल डिवाइस पर तैनात किया जाता है, तो यह काम नहीं करता है। –

+0

'zone.log ('zone', zone.current) जोड़ने का प्रयास करें; 'वर्तमान क्षेत्र की जांच करने के लिए। जोन वैश्विक चर है, क्या आपको इसका उपयोग करने के लिए टाइपिंग परिभाषा जोड़नी होगी। – kemsky

+0

देरी के लिए खेद है .... इसे वेब पर "कोणीय" के रूप में लॉग किया जा रहा है और माता-पिता जोन है। जब मैं कॉर्डोवा से कंसोल की जांच करता हूं, तो इसे "" के रूप में लॉग किया जाता है और माता-पिता शून्य होता है। –

संबंधित मुद्दे