Ember-Graph - An Ember-Data alternative

I’m going to try to answer all of your questions. If I miss one, let me know. :smile:

  1. To count items without loading them, use the relationship name prefixed with an underscore. So for you, object.get('_items') will return something like this:

    [
        { type: 'itemType', id: 'item-id-1' },
        { type: 'itemType', id: 'item-id-2' }
    }
    

    This won’t load any data from the server, so it is a synchronous call. As you’ll see from the example that I’ll post, this is handy when you need to do things like count the number of comments for a thread, without loading all of the comments.

  2. I just finished the first draft of a localStorage adapter on Thursday. It’s not perfect yet, but I plan on using it for integration testing for our Ember app, so it should progress fairly quickly. The goal is for the adapter to handle all relationship data for you, while at the same time, being flexible enough to implement custom ‘server’ logic. And because Ember-Graph doesn’t care where data comes from, it should be fairly easy to use both the localStorage adapter AND a REST adapter.

  3. For live server updates, I believe that Ember-Graph will be perfect. I’m sure it has flaws now, but this is exactly the thing Ember-Graph was designed to do. Ember-Graph is setup to receive updates from the server, even if your records are currently dirty. If you look at this (extremely long) file, you will see how much effort has gone into merging server and client side changes without creating conflicts.

    Also, I hope to have built-in Socket.IO support in the adapters soon.

  4. There is no embedded record support yet. There are a few reasons for that, but the biggest one is that I don’t know enough about embedded records to really implement the feature nicely. For instance, in your example, I wouldn’t call that an embedded record (since it doesn’t have an ID), I would call it an attribute. By extending the AttributeType class, you can create a QuotaType fairly easily. I do something like this for one of my models.

        App.QuotaType = EG.AttributeType.extend({
            serialize: function(quota) {
                return quota;
            },
            deserialize: function(json) {
                if (json && Em.typeOf(json.total) === 'number' && Em.typeOf(json.used) === 'number') {
                    return json;
                } else {
                    return { total: 100, used: 0 };
                }
            },
            // Ember-Graph uses this to only save changed attributes
            isEqual: function(a, b) {
                return (a && b && a.total === b.total && a.used === b.used);
            }
        });
    

    Then, you can change the values just like any other attribute:

    user.set('quota.used', user.get('quota.used') + 10);
    

Sorry, a bit of a long post, but hopefully I got your questions. If you have any feature requests, feel free to post them here or in Github Issues.

1 Like