WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

EmberJS Creating and Deleting Records


You can make and erase the records on the occurrence of model.

Syntax

import Ember from 'coal'; 

trade default Ember.Route.extend ({ 

display() { 

/code here 

}, 

actions:{ 

addNewCategory(id, name) { 

this.controller.get('model').pushObject({ var1,va2}); 

}, 

deleteCategory(category) { 

this.controller.get('model').removeObject(model_name); 

} 

} 

}); 

Example

The model given beneath shows creation and erasure of records. Make another course with the name record_demo and make one more course inside this course and name it as categories. Presently open the router.js document to characterize the URL mappings −

import Ember from 'ember'; 

//Access to Ember.js library as factor Ember 

import config from './config/environment'; 

//It gives access to application's setup information as factor config  

//The const announces read just variable 

const Router = Ember.Router.extend ({ 

location: config.locationType, 

rootURL: config.rootURL 

});
//Defines URL mappings that accepts parameter as an article to make the routes 

Router.map(function() { 

this.route('record_demo', function() { 

this.route('categories'); 

}); 

}); 

//It determines Router variable accessible to different parts of the app 

export default Router;

Open the application.hbs record made under app/layouts/ with the underneath code −

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

{{outlet}}

When you click the above connection, it will open the record_demo layout page, which is made under app/formats/. The record_demo.hbs document contains the fllowing code −

<h2>Welcome...Click the beneath connection for Categories page</h2> 

{{#link-to 'record_demo.categories'}}Go to Categories page{{/interface to}} 

{{outlet}}

The above format page opens the categories.hbs document, which is made under app/layouts/record_demo and contains the accompanying code −

<h2>Categories Page</h2> 

<form> 

<label>ID:</label> 

{{input value=newCategoryId}} 

<label>NAME:</label> 

{{input esteem = newCategoryName}} 

/when client includes records, the 'addNewCategory' work fires and includes 

the records to show 

<button type = "submit" {{action 'addNewCategory' newCategoryId newCategoryName}}> 

Add to list 

</button> 

</form> 

<ul> 

{{#each demonstrate as |category|}} 

<li> 

Id: {{category.id}}, Name: {{category.name}} 

/when client erase records, the 'deleteCategory' work fires and expel 

the records from model 

<button {{action 'deleteCategory' category}}>Delete</button> 

</li> 

{{/each}} 

</ul> 

/it checks the quantity of included records and expelled records from the model 

<strong>Category Counter: {{model.length}}</strong> 

{{outlet}}

Now open the categories.js document made under application/courses/record_demo with the accompanying code −

import Ember from 'ember'; 

export default Ember.Route.extend ({ 

model() { 

//model will show 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 expels the records from model 

deleteCategory(category) { 

this.controller.get('model').removeObject(category); 

} 

} 

});

Output

Run the coal server; you will get the accompanying yield −

Ember.js Create and Delete Records

When you click on the connection, it will open the records_demo page with the classifications page interface −

Ember.js Create and Delete Records

Next, the classes format page will open. Enter the id and name in the information box and snap the Add to list catch as appeared in the screen capture beneath −

Ember.js Create and Delete Records

Next, click on the include catch; you will see the additional records in the rundown and number of check will get augmented −

Ember.js Create and Delete Records

If you need to expel the records from the rundown, at that point click the Delete button.