Open dani3l opened 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:
@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.
@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.
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 || {};
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
.......
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:
$dirty
and$pristine
getters for model$modelErrors
is now$errors
_remoteState
is now$original
reset()
model method to reset back to original state$new()
collection method (since create saves the model right away, I needed an option to just create a new model)getOrQuery(id)
collection method to get a model from collection (and if not exists, try to get it from server and add to collection)$get(id)
collection method that always returns a promise (wrapper tocollection.get(id)
)find()
collection method (just a wrapper forcollection.where(options, true)
)neverSend
property when defining model, basically an array that says "never send those attributes to the server"getAttributes()
and added an option toincludeRelations
or not (false by default, so it won't send it to server when saving since it's an external model, and not test it when check if dirty or reseting to original).attr.date()
, so I kinda made it work, and I also use moment.js instead of vanilla dates_.
functions to__.
becuase I'm also using lodash in my projectand 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.