Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modella 0.2 support #7

Closed
scttnlsn opened this issue Oct 20, 2013 · 5 comments
Closed

Modella 0.2 support #7

scttnlsn opened this issue Oct 20, 2013 · 5 comments

Comments

@scttnlsn
Copy link

Since the static sync methods were removed from Modella this plugin needs to instantiate model instances from the result data before returning it from all and get.

One additional area that I think needs addressing is some sort of hook for parsing the data returned from the server. For example, a GET to /myresource might return something like:

{ "results": [...], "page": 1, ... }

And would need a corresponding function to parse out those results:

function parse(res) {
    return res.results;
}

There'd probably need to be 2 parse functions for get and all respectively. What are your thoughts here?

@rschmukler
Copy link
Member

My initial reaction is to have Modella-Ajax emit events that allow modifying of the data, much like Modella 0.2.0 emits setting and initializing. What do you think of that approach?

@scttnlsn
Copy link
Author

@rschmukler Can you give an example of how this might look? It's not obvious to me how the event callback would modify the response data.

@rschmukler
Copy link
Member

Sure, so maybe it'd be something like:

User.on('ajaxParseGetBody', function(res) {
  res.body.registeredAt = new Date(res.body.registeredAt);
});

Or perhaps more fitting to your exact example.

User.on('ajaxParseAllBody', function(res) {
    var users = res.body.results;
    // Convert JSON string dates into actual dates
    users.forEach(u) {
       u.registeredAt = new Date(u.registeredAt);
    }
    res.body = users;
});

And then, basically the code in Modella-ajax would look something like this:

request.get(url, function(res) {
    Model.emit('ajaxParseAllBody', res);
    var instances = new Model(res.body);
    ...
});

And then .get uses res.body to instantiate an instance of the User model. Does this make sense?

@scttnlsn
Copy link
Author

@rschmukler Yeah, that looks great.

@rschmukler
Copy link
Member

Closed w/ [#8]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants