-
Notifications
You must be signed in to change notification settings - Fork 19
Creating a New Resource
The initializer that is created when you use the ember generate resource
command sets up a factory in your application container. All the related factories/singletons are registered in your application container.
Using a protected group of routes that have features to administer content you can create new resources.
Here is an example methods for the model
hook which uses a resource factory object to create new record.
model() {
let owner = Ember.getOwner(this);
return owner._lookupFactory('model:post').create({ // †
isNew: true,
attributes: { date: new Date() }
});
},
† Note: eventually factoryFor
will replace _lookupFactory
.
A template of a form component can use the isNew
flag as well as any default attributes you set on the attributes
hash. When a resource has a true
value for its isNew
flag the events are not triggered to notify an adapter that the resource has changed attributes, as it's a new resource.
{{#if isNew}}
<p>
<label>Date:</label><br>
{{input type="date" value=resource.date name="date" placeholder="xx/xx/xxxx"}}
</p>
{{/if}}
- Admin/create template
<p>
<strong>Create a Blog Post</strong>
</p>
{{form-post post=model isNew=model.isNew on-save="save" on-cancel="cancel"}}
See below for source code examples for create a new post. The example code uses the same form-post
component to create and edit a resource. (Also the component uses buffering to delay the application of the changes to the resource instance.)