VasilioRuzanni / angular-modelizer

Simple and lightweight yet feature-rich models to use with AngularJS apps.
MIT License
26 stars 4 forks source link

Not an issue - just a thank you and changes I've made #4

Open dani3l opened 9 years ago

dani3l commented 9 years ago

Hi man! I wanted to thank you for this library. It saved me tons of hours figuring out how to make all data in sync. I ditched Restangular for this amazing lib and made a few changes:

and some more minor changes that I can't remember right now. I hope I haven't created memory leaks as I am not a js pro like you, but so far so good.

One thing that I also wanted to mention - it took me time to figure out what is going on in the file, it's a bit of a mess.

Anywho, this is my version, hope it might give you some ideas: https://gist.github.com/dani3l/3ba813373fd5849cff5d

I also created a simple updatedService to update/add/delete models in collection via websockets: ModelCreated: socket data: {id: id} -> client send request to server to get model and add to collection ModelUpdated: socket data {id: id, updatedAt: datetime} -> client check if has last version by comparing dates -> if not, get new version from server ModelDeleted: socket data: {id: id} -> client removed model from collection

I'm using Laravel 5.1 for the backend so this works really smooth.

VasilioRuzanni commented 9 years ago

@dani3l Thanks for the kind words! Actually, I like most of your changes a lot and I'm going to apply some of them.

Currently, I'm working towards complete refactoring, going to do few big changes:

dani3l commented 9 years ago

@VasilioRuzanni I'm anxious to see the end result :) glad I could help.

ES6 or TS would be awesome and much much more organized and make it easier to extend and keep things DRY - so thumbs up on that.

I'd also like to see option to globally extend model & collection if you have methods you often use.

VasilioRuzanni commented 9 years ago

@dani3l Well, thats something to consider indeed. Its easily doable using just regular JavaScript extending base Model since modelizer operates regular objects/arrays but I'll consider providing some simple API for that.

dani3l commented 9 years ago

Hi, just applied another fix, when using query(null, {params: { foo:'bar' }}) params are being ignored. So you can't all({params: ...})

query: function (queryParams, options) {
    options = options || {};
    options.params = __.extend({}, queryParams, options.params); // was: options.params = queryParams || {};
dani3l commented 9 years ago

Another suggestion - reset attributes after save let's say I have a User model and I change this password. to do that I set model.password = '123456' and save problem is, model.password never clears or changes since the server never returns the password.

To solve that I've added a 'resetAfterSave` option to model definition. When save completes, get the attributes to reset and reset them to modelDefinition value (In case you want to set a default value, or undefined if not set)

var promise = this.$request(reqOptions).then(function (resData) {
    .......

    // attributes to reset to original definition after save
    var resetAfterSave = _.union([], _this.resetAfterSave);
    var definition = _this._modelClassMeta.modelDefinition;
    var attrsToReset = {};

    angular.forEach(resetAfterSave, function(attribute) {
        attrsToReset[attribute] = angular.copy(definition[attribute]);
    });

    // Reset original
    .......