Ember.js 是一個開源 JavaScript 框架,用於開發基於 Model-View-Controller (MVC) 架構的大型客戶端 Web 應用程序。 Ember.js是使用最廣泛的前端應用框架之一。它的目的是加速開發並提高生產力。目前,許多網站都在使用它,包括 Square、Discourse、Groupon、Linked In、Live Nation、Twitch 和 Chipotle。
Route 類的 willDestroy() 方法是一個函數,每當分配給對象的所有資源都被釋放時,或者換句話說,當對象被銷毀時,就會調用該函數。
句法:
willDestroy(){ // Function Algorithm }
Parameters: 它不需要任何參數。
安裝和運行 Ember.js 的步驟:
第 1 步:要運行以下示例,您需要有一個 ember 項目。要創建一個,您需要先安裝ember-cli。在終端中寫入以下代碼:
npm install ember-cli
第 2 步:現在您可以通過輸入以下代碼來創建項目:
ember new <project-name> --lang en
要啟動服務器,請鍵入:
ember server
示例 1:鍵入以下代碼以生成本示例的路由:
ember generate route test1
應用程序/路線/test1.js
Javascript
import Route from "@ember/routing/route";
import EmberObject from '@ember/object';
import ObjectProxy from '@ember/array/proxy';
import { A } from '@ember/array';
export default class ListRoute extends Route {
object = EmberObject.create({
name: 'Sam Snehil',
Gender: 'M',
Occupation: 'Lawyer',
Salary: 89000,
mobile: 82982472928,
email: 'samsnehil@gmail.com',
willDestroy() {
console.log('Object is About to die!!!');
},
});
person = ObjectProxy.create(this.object
);
idx;
model() {
return this.person;
}
setupController(controller, model) {
super.setupController(controller, model);
controller.set("person", this.person);
controller.set("list1", this.list1);
}
}
應用程序/控製器/test1.js
Javascript
import Ember from "ember";
import {
clear, insertAt, indexOf, lastIndexOf,
includes
} from "@ember/array";
export default Ember.Controller.extend({
actions: {
delete() {
this.person.destroy();
},
showDestroyed() {
console.log(this.person.get('isDestroyed'))
}
},
});
應用程序/模板/test1.hbs
HTML
{{page-title "ObjectProxy"}}
<table>
<h1>Employee's Details:</h1>
<div><b>Name:</b> {{this.person.name}}</div>
<div><b>Gender:</b> {{this.person.Gender}}</div>
<div><b>Occupation:</b> {{this.person.Occupation}}</div>
<div><b>Mobile:</b> {{this.person.mobile}}</div>
<div><b>Email:</b> {{this.person.email}}</div>
<div><b>Salary:</b> {{this.person.Salary}}</div>
</table>
<br /><br/>
<input
type="button"
id="Destroy"
value="Release all Resource"
{{action 'delete'}}
/><br /><br/>
<input
type="button"
id="show"
value="show isDestroyed"
{{action 'showDestroyed'}}
/>
{{outlet}}
輸出:
輸出1
示例 2:鍵入以下代碼以生成本示例的路由:
ember generate route test2
應用程序/路線/test2.js
Javascript
import Route from '@ember/routing/route';
import ArrayProxy from '@ember/object';
import { computed } from '@ember/object';
const Person = ArrayProxy.extend({
fullName: computed('firstName', 'lastName', function () {
return this.get('firstName') + ' ' +
this.get('lastName');
}),
toStringExtension() {
return this.get('fullName');
}
});
export default class StudentsRoute extends Route {
person = Person.create({
firstName: 'Aman',
lastName: 'Verma',
gender: 'M',
class: 10,
marks: 83,
mobile: '8399384948',
grade: 'A',
willDestroy() {
console.log('Object is About to die!!!');
}
})
model() {
return this.person;
}
setupController(controller, model) {
super.setupController(controller, model);
controller.set('person', this.person);
}
}
應用程序/控製器/test2.js
Javascript
import Ember from 'ember';
import EmberObject from '@ember/object';
import { cacheFor, destroy } from '@ember/object';
export default Ember.Controller.extend({
actions: {
des() {
let t1 = this.person.destroy();
},
print() {
console.log('Value of isDestroyed : '
+ this.person.isDestroyed)
},
}
});
應用程序/模板/test2.js
HTML
{{page-title "ObjectProxy "}}
<table>
<h1>Student's Details:</h1>
<div><b>Name:</b> {{this.person.fullName}}</div>
<div><b>Gender:</b> {{this.person.gender}}</div>
<div><b>Class:</b> {{this.person.class}}</div>
<div><b>Mobile:</b> {{this.person.mobile}}</div>
<div><b>Grade:</b> {{this.person.grade}}</div>
<div><b>Marks:</b> {{this.person.marks}}</div>
</table>
<br /><br/>
<input
type="button"
id="Destroy"
value="Destroy Object"
{{action 'des'}}
/><br /><br/>
<input
type="button"
id="CacheFor"
value="isDestroyed"
{{action 'print'}}
/>
{{outlet}}
輸出:
輸出2
參考:https://api.emberjs.com/ember/4.9/classes/Route/methods/willDestroy?anchor=willDestroy
相關用法
- Embeer.js Route init()用法及代碼示例
- Embeer.js Route toString()用法及代碼示例
- Embeer.js Route setProperties()用法及代碼示例
- Embeer.js Route getProperties()用法及代碼示例
- Embeer.js Route decrementProperty()用法及代碼示例
- Embeer.js Route templateName用法及代碼示例
- Embeer.js Route get()用法及代碼示例
- Embeer.js Route addObserver()用法及代碼示例
- Embeer.js Route setupController()用法及代碼示例
- Embeer.js Route set()用法及代碼示例
- Embeer.js RouterService replaceWith()用法及代碼示例
- Embeer.js RouterService rootURL用法及代碼示例
- Embeer.js RouterService mergedProperties用法及代碼示例
- Embeer.js RouteInfo parent用法及代碼示例
- Embeer.js RouterService setProperties()用法及代碼示例
- Embeer.js RouteInfoWithAttributes name用法及代碼示例
- Embeer.js RouterService incrementProperty()用法及代碼示例
- Embeer.js RouterService decrementProperty()用法及代碼示例
- Embeer.js RouterService init()用法及代碼示例
- Embeer.js RouterService get()用法及代碼示例
- Embeer.js Promise then()用法及代碼示例
- Embeer.js ComputedProperty readOnly()用法及代碼示例
- Embeer.js Controller toString()用法及代碼示例
- Embeer.js Transition then()用法及代碼示例
- Embeer.js Transition data用法及代碼示例
注:本文由純淨天空篩選整理自satyam00so大神的英文原創作品 Ember.js Route willDestroy() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。