Skip to content

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

@dani3l

Description

@dani3l

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 to collection.get(id))
  • find() collection method (just a wrapper for collection.where(options, true))
  • neverSend property when defining model, basically an array that says "never send those attributes to the server"
  • I've changed getAttributes() and added an option to includeRelations 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).
  • I've changed the modelErrors to match my errors array from server (error: [attribute: [required, unique]])
  • There was a bug with the attr.date(), so I kinda made it work, and I also use moment.js instead of vanilla dates
  • changed _. functions to __. becuase I'm also using lodash in my project

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions