當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Embeer.js ArrayProxy destroy()用法及代碼示例


Ember.js 是一個開源 JavaScript 框架,用於開發基於 Model-View-Controller (MVC) 架構的大型客戶端 Web 應用程序。 Ember.js是使用最廣泛的前端應用框架之一。它的目的是加速開發並提高生產力。目前,它被大量網站使用,包括 Square、Discourse、Groupon、Linked In、Live Nation、Twitch 和 Chipotle。

ArrayProxy的destroy()方法用於銷毀該對象。此方法通過設置 isDestroyed 標誌來銷毀對象並刪除其所有元數據。

句法:

this.Object.destroy() ; 

Parameters: 它不需要任何參數。

返回類型:該方法返回一個對象。

安裝和運行 Ember.js 的步驟:

第 1 步:要運行以下示例,您需要有一個 ember 項目。要創建一個,您需要先安裝ember-cli。在終端中寫入以下代碼:

npm install ember-cli

第 2 步:現在您可以通過輸入以下代碼來創建項目:

ember new <project-name> --lang en

要啟動服務器,請鍵入:

ember serve

示例 1:鍵入以下代碼以生成本示例的路由:

ember generate route temp1

應用程序/路線/temp1.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', 
  
    }) 
  
    model() { 
        return this.person; 
    } 
    setupController(controller, model) { 
        super.setupController(controller, model); 
        controller.set('person', this.person); 
    } 
}

應用程序/控製器/temp1.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(); 
            console.log(t1 + ' is Destroyed ') 
        }, 
        print() { 
            console.log('Value of isDestroyed : ' 
                + this.person.isDestroyed) 
        }, 
    } 
}); 

應用程序/模板/temp1.hbs

HTML


{{page-title "ArrayProxy"}} 
<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="CacheFor"
    value="Destroy Details"
    {{action 'des'}} 
/><br /><br/> 
<input
    type="button"
    id="CacheFor"
    value="Print Ember Class"
    {{action 'print'}} 
/> 
{{outlet}}

輸出:

destroy()方法輸出1

示例 2:鍵入以下代碼以生成本示例的路由:

ember generate route temp2

應用程序/路線/temp2.js

Javascript


import Route from '@ember/routing/route'; 
import EmberObject from '@ember/object'; 
import ArrayProxy from '@ember/object/proxy'; 
  
const Employee = ArrayProxy.extend({ 
    toStringExtension() { 
        return this.get('fullName'); 
    } 
}); 
  
export default class DetailsRoute extends Route { 
    person; 
    createPerson() { 
        let person = Employee.create({ 
            name: 'Rahul Singh', 
            age: 24, 
            mobile: '1298119967', 
            country: 'India', 
            language: { 
                0: ['Bengali'] 
            }, 
            salary: 10000, 
            gender: 'M', 
            zipCode: '800020', 
        }); 
        return person 
    } 
  
    model() { 
        this.person = this.createPerson(); 
        return this.person 
    } 
    setupController(controller, model) { 
        super.setupController(controller, model); 
        controller.set('person', this.person); 
    } 
} 

應用程序/控製器/temp2.js

Javascript


import Ember from 'ember'; 
import EmberObject from '@ember/object'; 
  
const Employee = EmberObject.extend({ 
    concatenatedProperties: ['work', 'contact'], 
    work: { 
        0: ['Developing Natural Language Processing'] 
    }, 
    contact: { 
        phone: ['9839281832'] 
    } 
}); 
export default Ember.Controller.extend({ 
  
    actions: { 
  
        des() { 
            let t1 = this.person.destroy(); 
            console.log(t1 + ' is Destroyed ') 
        }, 
        print() { 
            console.log('Value of isDestroyed : '
                + this.person.isDestroyed) 
        }, 
        IncreaseSalary() { 
            let temp = this.person.salary + 1000; 
            this.person.set('salary', temp); 
        } 
    } 
});

應用程序/模板/temp2.hbs

HTML


{{page-title "ArrayProxy"}} 
<table> 
    <h1>Employee's Details: </h1> 
    <div><b>Name:</b> {{this.person.name}}</div> 
    <div><b>Gender:</b> {{this.person.gender}}</div> 
    <div><b>Salary:</b> {{this.person.salary}}</div> 
    <div><b>Mobile No.:</b>{{this.person.mobile}}</div> 
    <div><b>Zip Code:</b> {{this.person.zipCode}}</div> 
    <div><b>Country:</b> {{this.person.country}}</div> 
    <div><b>Age:</b> {{this.person.age}}</div> 
    <div><b>Work Under:</b> 
        {{#each-in this.person.language as |key value|}} 
        <li>{{value}}</li> 
        {{/each-in}} 
    </div> 
</table> 
  
<br /><br/> 
<input
    type="button"
    id="CacheFor"
    value="Destroy Details"
    {{action 'des'}} 
/><br /><br/> 
<input
    type="button"
    id="CacheFor"
    value="Print isDestroyed Property"
    {{action 'print'}} 
/> 
<br /><br/> 
<input
    type="button"
    id="Change"
    value="Increase Salary"
    {{action 'IncreaseSalary'}} 
> 
{{outlet}}

輸出:

destroyed()方法輸出

參考:https://api.emberjs.com/ember/4.9/classes/ArrayProxy/methods/destroy?anchor=destroy



相關用法


注:本文由純淨天空篩選整理自satyam00so大神的英文原創作品 Ember.js ArrayProxy destroy() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。