Skip to content

Add createInstance for resources with models #93

Closed
@kentcdodds

Description

@kentcdodds

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?

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions