angularfire icon indicating copy to clipboard operation
angularfire copied to clipboard

Add get data as array option

Open tamtakoe opened this issue 12 years ago • 16 comments

Getting collection ad object is very unconfortable. Use orderByPriority too. Make option that collection get always like array.

Also make API-compatible resourse with angular resource

tamtakoe avatar Jan 03 '14 08:01 tamtakoe

You get the data as an array using orderByPriority - does this not help your particular use case?

I'm also curious about why it feels uncomfortable to receive an object instead of an array. Thanks for your feedback!

anantn avatar Jan 03 '14 17:01 anantn

Using array for collection is habitual. F.e. if I also use angular resource in my project I need remember about data type and use orderByPriority for firebase collection. If i need change resource collection to firebase collection I have some trouble.

I made helper

app.factory('$firebaseArr', ['$firebase', '$filter', function($firebase, $filter) {
    return function(ref) {
        var dataObj = $firebase(ref);
        var dataArr = angular.extend([], dataObj);

        dataObj.$on('change', function() {
            dataArr.length = 0;
            angular.extend(dataArr, $filter('orderByPriority')(dataObj));
        });
        return dataArr;
    }
}]);

tamtakoe avatar Jan 09 '14 06:01 tamtakoe

I second tamtakoe's opinion. When working with a datamodel, a list of objects (by there key) is very "unnatural".

I have my firebase organized in a way a sql database is organized, e.d. for each "table" (collection) a separate Url. When getting "the table" a want to loop over the items to do somthing with them (filter, sort etc)

JohnBerg60 avatar Jan 09 '14 11:01 JohnBerg60

I just started trying to convert my AngularJS app which uses HTML5 LocalStorage to use AngularFire instead and was stymied by trying to do 3-way binding of arrays. I'm going to see if I can use the factory that @tamtakoe mentioned above but otherwise I guess I'll have to do it manually.

doxxx avatar Jan 18 '14 05:01 doxxx

One important thing to keep in mind is that arrays use numeric, sequential keys. That's great for static data, but not so great for real-time collaborative data sets. There's really no comparison to SQL here as that has none of the real time aspects of Firebase.

Even if angularFire supported arrays, you'd still run into lots of edge cases where data is deleted/added/modified/replaced incorrectly because concurrent user edits went into the same key in the array.

Consider also that, internally, Firebase stores everything as an object. So what would it mean if you do array.sort() or array.splice() on the client? Those couldn't apply to the remote data (keyed by unique ids) and certainly couldn't survive a concurrent event from two users at once--reconciling keys and data would be quite impossible.

These are a few of the reasons that arrays are not as simple and straightforward as they might first appear.

Feel free to offer feedback on your specific use cases and how you'd like to see such aspects handled for your needs.

katowulf avatar Apr 01 '14 00:04 katowulf

Hi @katowulf thanks for the heads up, would you also have any idea of a best practice for using ui-sortable with angularfire?

gaiapunk avatar Apr 01 '14 00:04 gaiapunk

I think once we solve how best to handle arrays, it will also resolve ui-sortable issues. It's simply a matter of finding the API enhancements that best serve the Angular-verse.

katowulf avatar Apr 01 '14 00:04 katowulf

Thanks @katowulf I sent you 2 Flattr payments for your super responsive help :)

gaiapunk avatar Apr 01 '14 00:04 gaiapunk

@katowulf I don't have any suggestions myself but perhaps you would be interested in seeing the issue so as to determine the best course of action as I'm sure many Angular devs will eventually encounter this conundrum. My business partner showed me that everything appears to work mostly fine with the firebase backend but for some yet inexplicable reason data disappears from the front end.

gaiapunk avatar Apr 04 '14 14:04 gaiapunk

Will have a $[get?]asArray() method in 0.8.0 which will address this.

katowulf avatar Apr 15 '14 18:04 katowulf

Thank You SO Much Katowulf!

Firebase FTW!

On Tuesday, April 15, 2014, katowulf [email protected] wrote:

Closed #200 https://github.com/firebase/angularFire/issues/200.

— Reply to this email directly or view it on GitHubhttps://github.com/firebase/angularFire/issues/200 .

gaiapunk avatar Apr 15 '14 20:04 gaiapunk

By chance doo you have any ball-park estimate on when that version will launch?

gaiapunk avatar Apr 16 '14 06:04 gaiapunk

I've posted a gist at the linked issue, that works with moving items between 2 $firebaseArrays within ui-sortable.

I'm not sure it's the best practice, and from intense googling @katowulf is the expert on firebaseArray issues. It would be supercool if kato could review if that's a good way of handling the firebaseArrays then I think we'd be a step closer for native firebase support in ui.sortables :-)

Basically removed any slice options, except one since it's used only for re-ordering of a single list, and later on priorities are updated anyway based on indexOf position.

mastef avatar Dec 30 '15 09:12 mastef

Thanks! I've put this on my todo list. There's a lot to read through here to get up to speed on all the nuances and possible solutions. Since the diff of the gist is mostly green, could you highlight the key points? That would help shave a bit of angle off of the learning curve.

katowulf avatar Dec 30 '15 17:12 katowulf

Could you also open a separate issue rather than multi-purposing a closed, old task? This one is technically from the alpha version of AngularFire and about retrieving a synchronized array (i.e. implementing $firebaseArray() was the solution to this issue).

katowulf avatar Dec 30 '15 17:12 katowulf

Hey @katowulf thanks for the reply and happy new year! I've posted it into #687

mastef avatar Jan 03 '16 12:01 mastef