Skip to content

Creating a New Resource

Bill Heaton edited this page Jun 16, 2015 · 5 revisions

Cookbook for creating records

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.

Admin Route

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() {
    return this.container.lookup('model:posts').create({
      isNew: true,
      attributes: { date: new Date() }
    });
  },

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.)

Links

Clone this wiki locally