2014-09-04 51 views
1

根據Angularfire文檔,當使用通過$asObject()返回的對象時,可以通過在對象上定義$priority屬性來設置該對象的優先級,然後使用$save()

我的代碼效果很好,但$priority沒有做任何事情。下面是一些代碼完全解釋的評論:

app.factory('MyService', function($rootScope, $firebase) { 

    // The complete Firebase url 
    var ref = *obfuscated*; 

    // Returning the dataset as an object containing objects 
    var data = $firebase(ref).$asObject; 

    // This object is what's returned by MyService 
    var Data = { 

    // Method to create a new object within the data set, 
    // keyed by myId. Attempting to set priority for the 
    // record via $priority. returnData.uid is a valid string. 
    create: function(returnData, myId) { 
     data[myId] = { 
     myId: myId, 
     $priority: returnData.uid 
     }; 

     // No need to explain the stuff between here and the 
     // $rootScope listener below, just added for context 
     data.$save().then(function() { 
     setMyId(myId); 
     }); 
    }, 
    findByMyId: function(myId) { 
     if (myId) { 
     return data[myId]; 
     } 
    } 
    }; 

    function setMyId(myId) { 
    $rootScope.myId = User.findByMyId(myId); 
    } 

    // This event listener works fine, fires 
    // at user login and returns data 
    $rootScope.$on('$firebaseSimpleLogin:login', function(e, returnData) { 

    // returnData.uid has the correct value - this 
    // function should return the object(aka record) with 
    // a priority matching returnData.uid 
    var query = $firebase(ref.startAt(returnData.uid).endAt(returnData.uid)).$asObject(); 

    // console shows an object with the normal $firebase 
    // properties, but no records. If I define query without 
    // limiting the set ($firebase(ref).$asObject()), it returns 
    // the full set just fine. The priority on all records is still 
    // null. 
    console.log(query);  

    query.$loaded(function() { 
     setData(query.myId); 
    }); 
    }); 

    return Data; 
}); 

是的,我下面Thinkster.io的教程,我在第7章不,這不是關於章其他問題重複,我已經找到了他們示例中的Angularfire 0.8之前版本的代碼,只是無法設置$ priority,至今我花了大約5個小時試圖通過我自己的努力和網絡找到解決方案。

任何接受者?

+2

您不能在對象的子節點上使用$ priority;你可以在根級上設置;這與Firebase.set/setWithPriority行爲相同。你正在尋找$ asArray(),這將允許你管理一個有序的集合。 – Kato 2014-09-04 23:10:37

+0

@Kato Man - 你如何從文檔中知道?這是門票,但謝謝你的迴應。如果你添加這個答案,我會標記它。我相信其他人將會尋找這個信息。 – 2014-09-05 00:37:38

+1

如果您意識到優先級(至少在邏輯上)作爲屬性本身進行存儲,則可能會有所幫助。所以你只能給一個對象/非葉節點添加一個優先級,而不是一個屬性本身。 – 2014-09-05 01:06:27

回答

3

當在JavaScript的工作方式與對象(即無序)的光下觀察,如何JSON處理對象(即無序),並在預期的光認爲AngularFire的$asObject()方法旨在for storing key/value pairs, and singular records that are not used as a collection,這開始讓一些感覺。

在內部,同步對象的$save方法調用Firebase的setWithPriority。在setsetWithPriority調用中,子節點被替換。任何元數據,如這些孩子的優先級被替換。

在AngularFire中,$asArray旨在處理有序集合,並提供在子節點上設置$priority(當然,因爲它將子對象視爲不是自己集合的單數記錄)。

因爲在你的情況,你想與固定鍵工作,而不是推的ID,你可能會想重寫$使用$ extendFactory添加方法和做類似如下:

angular.module(MY_APP).factory('FixedKeysFactory', function($FirebaseArray, $firebaseUtils) { 
    return $FirebaseArray.$extendFactory({ 
    $add: function(data) { 
     this._assertNotDestroyed('$add'); 
     if(angular.isObject(data) && typeof data.$id === 'string') { 
     return this.$inst().$set(data.$id, $firebaseUtils.toJSON(data)); 
     } 
     else { 
     return this.$inst().$push($firebaseUtils.toJSON(data)); 
     } 
    } 
    }); 
}); 

然後,您可以傳遞到$火力比如這地方出廠默認的:

var list = $firebase(ref, {arrayFactory: FixedKeysFactory}).$asArray(); 

一個簡單但不太awesomatic™解決方案將是你的對象手動添加到陣列中,手動給他們的$ id,然後請致電$ save:

var list = $firebase(ref).$asArray(); 
var i = list.length; 
list.push({ foo: 'bar', $id: 'kato' }); 
list.$save(i); 

關於未來的一些說明:很快就有可能使用任何字段作爲排序標準,並且不需要設置優先級(yay!)。在我用其他開發人員(如0.8.3版本)清除它之前,可以在調用$ add在AngularFire中的同步數組之前設置自己的$ id。

+1

真棒,真棒,真棒,很好的答案 - 謝謝。 – 2014-09-05 16:48:02