lib/modelItem.js
function modelItem(data, model) {
    var res = {};

    utils.defineProperties(res, {
        _isModelItem: { value: true },

        model: { value: model.value },

        update: { value: function(data, sync) {
            if (sync == null) sync = true;

            return update(res, model, data).then(function() {
                if (!sync) return;
                return res.save();
            });
        } },

        save: { value: function() {
            if (_.has(res, 'id')) {
                var props = serverProperties(res, model);
                return utils.socketPost(model.io, model.url + '/update/' + res.id, props.properties).then(function() {
                    return Promise.all(_.map(props.refresh, function(item) {
                        return item.refresh();
                    }));
                });
            }
            return create(res, model);
        } },

        refresh: { value: function() {
            if (!_.has(res, 'id')) return create(res, model);

            return utils.socketGet(model.io, model.url + '/' + res.id).then(function(data) {
                return update(res, model, data, { noRefresh: true });
            });
        } },

        delete: { value: function() {
            model._removeItem(res);
            return utils.socketDelete(model.io, model.url, { id: res.id });
        } },

        matches: { value: function(query) {
            return utils.socketGet(model.io, model.url, query).then(function(r) {
                for (var i = 0; i < r.length; i++) {
                    if (r[i].id === res.id) return true;
                }
                return false;
            });
        } },

        then: { value: function() {
            return res.ready.then.apply(res.ready, arguments);
        } },

        catch: { value: function() {
            return res.ready.catch.apply(res.ready, arguments);
        } }
    });

    res.ready = Promise.resolve();

    update(res, model, data || {});

    return res;
}
function modelItem(data, model) {
    var res = {};

    utils.defineProperties(res, {
        _isModelItem: { value: true },

        model: { value: model.value },

        update: { value: function(data, sync) {
            if (sync == null) sync = true;

            return update(res, model, data).then(function() {
                if (!sync) return;
                return res.save();
            });
        } },

        save: { value: function() {
            if (_.has(res, 'id')) {
                var props = serverProperties(res, model);
                return utils.socketPost(model.io, model.url + '/update/' + res.id, props.properties).then(function() {
                    return Promise.all(_.map(props.refresh, function(item) {
                        return item.refresh();
                    }));
                });
            }
            return create(res, model);
        } },

        refresh: { value: function() {
            if (!_.has(res, 'id')) return create(res, model);

            return utils.socketGet(model.io, model.url + '/' + res.id).then(function(data) {
                return update(res, model, data, { noRefresh: true });
            });
        } },

        delete: { value: function() {
            model._removeItem(res);
            return utils.socketDelete(model.io, model.url, { id: res.id });
        } },

        matches: { value: function(query) {
            return utils.socketGet(model.io, model.url, query).then(function(r) {
                for (var i = 0; i < r.length; i++) {
                    if (r[i].id === res.id) return true;
                }
                return false;
            });
        } },

        then: { value: function() {
            return res.ready.then.apply(res.ready, arguments);
        } },

        catch: { value: function() {
            return res.ready.catch.apply(res.ready, arguments);
        } }
    });

    res.ready = Promise.resolve();

    update(res, model, data || {});

    return res;
}
function modelItem ( data , model ) { var res = { } ; utils . defineProperties ( res , { _isModelItem : { value : true } , model : { value : model . value } , update : { value : function ( data , sync ) { if ( sync == null ) sync = true ; return update ( res , model , data ) . then ( function ( ) { if ( ! sync ) return ; return res . save ( ) ; } ) ; } } , save : { value : function ( ) { if ( _ . has ( res , 'id' ) ) { var props = serverProperties ( res , model ) ; return utils . socketPost ( model . io , model . url + '/update/' + res . id , props . properties ) . then ( function ( ) { return Promise . all ( _ . map ( props . refresh , function ( item ) { return item . refresh ( ) ; } ) ) ; } ) ; } return create ( res , model ) ; } } , refresh : { value : function ( ) { if ( ! _ . has ( res , 'id' ) ) return create ( res , model ) ; return utils . socketGet ( model . io , model . url + '/' + res . id ) . then ( function ( data ) { return update ( res , model , data , { noRefresh : true } ) ; } ) ; } } , delete : { value : function ( ) { model . _removeItem ( res ) ; return utils . socketDelete ( model . io , model . url , { id : res . id } ) ; } } , matches : { value : function ( query ) { return utils . socketGet ( model . io , model . url , query ) . then ( function ( r ) { for ( var i = 0 ; i < r . length ; i ++ ) { if ( r [ i ] . id === res . id ) return true ; } return false ; } ) ; } } , then : { value : function ( ) { return res . ready . then . apply ( res . ready , arguments ) ; } } , catch : { value : function ( ) { return res . ready . catch . apply ( res . ready , arguments ) ; } } } ) ; res . ready = Promise . resolve ( ) ; update ( res , model , data || { } ) ; return res ; }
A single model item that is synced with the server

@param {object} data
@param {Model} model
A single model item that is synced with the server