Emberjs-model-create-del-records

提供:Dev Guides
移動先:案内検索

EmberJS-レコードの作成と削除

モデルのインスタンスでレコードを作成および削除できます。

構文

import Ember from 'ember';

export default Ember.Route.extend ({
   model() {
     //code here
   },
   actions:{
      addNewCategory(id, name) {
         this.controller.get('model').pushObject({ var1,va2});
      },
      deleteCategory(category) {
         this.controller.get('model').removeObject(model_name);
      }
   }
});

以下の例は、レコードの作成と削除を示しています。 _record_demo_という名前の新しいルートを作成し、このルート内にもう1つのルートを作成して、_categories_という名前を付けます。 _router.js_ファイルを開いて、URLマッピングを定義します-

import Ember from 'ember';
//Access to Ember.js library as variable Ember
import config from './config/environment';
//It provides access to app's configuration data as variable config

//The const declares read only variable
const Router = Ember.Router.extend ({
   location: config.locationType,
   rootURL: config.rootURL
});

//Defines URL mappings that takes parameter as an object to create the routes
Router.map(function() {
   this.route('record_demo', function() {
      this.route('categories');
   });
});

//It specifies Router variable available to other parts of the app
export default Router;

以下のコードで_app/templates/_の下に作成された_application.hbs_ファイルを開きます-

{{#link-to 'record_demo'}}Go to Records demo page{{/link-to}}
{{outlet}}

上記のリンクをクリックすると、_app/templates/_の下に作成されるrecord_demoテンプレートページが開きます。 _record_demo.hbs_ファイルには以下のコードが含まれています-

<h2>Welcome...Click the below link for Categories page</h2>
{{#link-to 'record_demo.categories'}}Go to Categories page{{/link-to}}
{{outlet}}

上記のテンプレートページは、_app/templates/record_demo_の下に作成され、次のコードが含まれるCategories.hbsファイルを開きます-

<h2>Categories Page</h2>
<form>
   <label>ID:</label>
   {{input value=newCategoryId}}
   <label>NAME:</label>
   {{input value = newCategoryName}}
  //when user adds records, the 'addNewCategory' function fires and adds
      the records to model
   <button type = "submit" {{action 'addNewCategory' newCategoryId newCategoryName}}>
      Add to list
   </button>
</form>

<ul>
   {{#each model as |category|}}
      <li>
         Id: {{category.id}}, Name: {{category.name}}
        //when user delete records, the ‘deleteCategory’ function fires and remove
            the records from model
         <button {{action 'deleteCategory' category}}>Delete</button>
      </li>
   {{/each}}
</ul>

//it counts the number of added records and removed records from the model
<strong>Category Counter: {{model.length}}</strong>
{{outlet}}

次のコードでapp/routes/record_demoの下に作成されたcategories.jsファイルを開きます-

import Ember from 'ember';

export default Ember.Route.extend ({
   model() {
     //model will display these records when you execute the code
      return [{
         id: 1,
         name: 'Category One'
      }, {
         id: 2,
         name: 'Category Two'
      }];
   },

   actions: {
     //it adds records to model
      addNewCategory(id, name) {
         this.controller.get('model').pushObject({id,name});
      },

     //it removes the records from model
      deleteCategory(category) {
         this.controller.get('model').removeObject(category);
      }
   }
});

出力

emberサーバーを実行します。次の出力が表示されます-

Ember.jsレコードの作成と削除

リンクをクリックすると、カテゴリページのリンクを含むrecords_demoページが開きます-

Ember.jsレコードの作成および削除

次に、カテゴリテンプレートページが開きます。 入力ボックスにIDと名前を入力し、下のスクリーンショットに示すように[リストに追加]ボタンをクリックします-

Ember.jsレコードの作成と削除

次に、追加ボタンをクリックします。リストに追加されたレコードが表示され、カウント数が増加します-

Ember.jsレコードの作成および削除

リストからレコードを削除する場合は、[削除]ボタンをクリックします。