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

Add createInstance for resources with models #93

Closed
kentcdodds opened this issue Jul 17, 2014 · 8 comments
Closed

Add createInstance for resources with models #93

kentcdodds opened this issue Jul 17, 2014 · 8 comments
Assignees
Milestone

Comments

@kentcdodds
Copy link
Contributor

I have this code:

    // All resources should be defined at this point
    var resourceNames = Object.keys(DS.definitions);
    // Add common meta data on all definitions
    angular.forEach(resourceNames, function(resourceName) {
      var definition = DS.definitions[resourceName];
      definition.meta = definition.meta || {};

      // add createInstance
      var ResourceClass = definition[definition.class] || Object;
      definition.meta.createInstance = function(attrs) {
        var instance = new ResourceClass();
        return angular.extend(instance, attrs);
      };
    });

This allows me to do this:

DS.definitions.user.meta.createInstance({
  id: 'SOME_RANDOM_ID',
  name: 'A Name',
  otherStuff: 324
});

Seems like this would be a common use case. Would be nice if it were baked in. What do you think?

@jmdobry
Copy link
Member

jmdobry commented Jul 17, 2014

So:

var user = DS.createInstance('user', attrs);

as sugar for:

var user = new DS.definitions.user[DS.definitions.user.class](attrs);

@kentcdodds
Copy link
Contributor Author

👍 exactly

@jmdobry
Copy link
Member

jmdobry commented Jul 17, 2014

I like it.

@jmdobry jmdobry self-assigned this Jul 17, 2014
@jmdobry jmdobry added this to the 0.10.1 milestone Jul 18, 2014
@kentcdodds
Copy link
Contributor Author

Hmmm, in trying to upgrade I ran into a snag with this solution. I have a function that takes a definition name, but sometimes that name is the plural form of the resource name (ie companies vs company) so what I do is attempt to get the definition from DS.definitions based on the singular version of what's given and then I have some custom mapping (like "toUser" is considered "user") as well to get the definition.

Adding createInstance on DS and requiring the definition name sort of breaks what I was trying to accomplish. So I think what I'll have to do is first identify the resource name based on whether there's a definition for what's given, and then I can use DS.createInstance with that resource name...

Thanks for letting me talk through this... I think I have a good solution, but it may have been nice to have createInstance on the definition as well... Just a thought :-)

@kentcdodds
Copy link
Contributor Author

Just realized this is in version 0.10.1, and there's no tag for that yet. So I can't use it quite yet. But that's ok because it allows you to make this change easier (if that's something you want to do). What do you think?

@jmdobry
Copy link
Member

jmdobry commented Jul 19, 2014

Adding createInstance on DS and requiring the definition name

This was my original proposal, but I can add a method on the resource definition that wraps the DS.createInstance call. In fact, it might be nice for the resource definition object to wrap all DS methods that require a resource name. I'll change defineResource to return the definition object (it should have done that already). e.g.:

var User = DS.defineResource({ name: 'user', ... });

User.find(5).then(...); // Same as DS.find('user', 5)
User.findAll({...}).then(...); // Same as DS.findAll('user', {...})
User.get(5); // Same as DS.get('user', 5)
User.filter({...}); // Same as DS.filter('user', {...})
User.createInstance({...}); // Same as DS.createInstance('user', {...})

@kentcdodds
Copy link
Contributor Author

Yeah, I didn't realize that your proposal wouldn't work with how I implemented it. But I got around it pretty easily.

That solution actually sounds amazing. I think it would make things easier to work with.

@jmdobry
Copy link
Member

jmdobry commented Jul 20, 2014

Available as of 0.10.1.

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

No branches or pull requests

2 participants