aarondfrancis / vue-model

Model component for Vue.js
MIT License
855 stars 42 forks source link

adding an offline storage would be nice #1

Closed Pandahisham closed 7 years ago

aarondfrancis commented 8 years ago

@Pandahisham Interesting, care to elaborate a little more?

Pandahisham commented 8 years ago

http://gwendall.github.io/way/

atinux commented 8 years ago

I thought about it, and I think, to let the developers use their own solution without making you @aarondfrancis adding this feature (making the code bigger and maybe not useful for other developers): Why not letting a custom function in the instance actions options?

Example:

actions: {
    list: function (callback) {
        callback(null, localStorage);
    },
    create: function (data, callback) {
        localStorage.setItem(data.id, JSON.stringify(data));
        callback(null, data);
    },
    fetch: function (id, callback) {
        var data = localStorage.getItem(id);
        if (!data) return callback({ message: 'No model found with ID ['+id+']');
        callback(null, data);
    },
    // ...
}

If the method in actions is a function (instead of an object), call-it as it's called with it args + a callback, and depending of the arguments sent to the callback, keep the same logic you already have:

self.emit(name + '.success', {
    sent: sent,
    received: data
});
api.editing = false;

So your solution is flexible and the developers can use their own solution (localStorage, sockets, etc.) for each action.

It would be nice to keep the logic with the validation, validationErrors... and also have access to this.type to be able to call specific functions depending of the model name.

Hope it would help you 😄

atinux commented 8 years ago

Or no argument in the functions, only the callback. And we can have access to this.data for accessing the model data. This way, in the fetch or save, we can use directly the data from the context.

aarondfrancis commented 8 years ago

@Atinux this is a great idea to keep the plugin extremely flexible and also very manageable for me. Thanks for the well thought out suggestion. This should also allow @Pandahisham to satisfy his LocalStorage requirement.

Will implement. 😄