当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Embeer.js ObjectProxy isDestroyed用法及代码示例


Ember.js 是一个开源 JavaScript 框架,用于开发基于 Model-View-Controller (MVC) 架构的大型客户端 Web 应用程序。 Ember.js是使用最广泛的前端应用框架之一。它的目的是加速开发并提高生产力。目前,许多网站都在使用它,包括 Square、Discourse、Groupon、Linked In、Live Nation、Twitch 和 Chipotle。

ObjectProxy 的 isDestroyed 属性返回一个布尔值,指示对象代理是否已被销毁。

用法:

object.isDestroyed ; 

Parameters: 它不需要任何参数。

安装和运行 Ember.js 的步骤:

第 1 步:要运行以下示例,您需要有一个 ember 项目。要创建一个,您需要先安装ember-cli。在终端中写入以下代码:

npm install ember-cli

第 2 步:现在您可以通过输入以下代码来创建项目:

ember new <project-name> --lang en

要启动服务器,请键入:

ember server

示例 1:键入以下代码以生成本示例的路由:

ember generate route file1

应用程序/路线/file1.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); 
    } 
}

应用程序/控制器/file1.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) 
        }, 
  
    } 
}); 

应用程序/模板/file1.hbs

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}}

输出:

输出1

示例 2:键入以下代码以生成本示例的路由:

ember generate route file2

应用程序/路线/file2.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); 
    } 
}

应用程序/控制器/file2.js

Javascript


import Ember from "ember"; 
import { 
    destroy 
} from "@ember/array"; 
  
export default Ember.Controller.extend({ 
    actions: { 
        delete() { 
            this.person.destroy(); 
        }, 
        showDestroyed() { 
            console.log(this.person.get('isDestroyed')) 
        } 
    }, 
}); 

应用程序/模板/file2.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}}

输出:

输出2

参考:https://api.emberjs.com/ember/4.9/classes/ObjectProxy/properties/isDestroyed?anchor=isDestroyed



相关用法


注:本文由纯净天空筛选整理自satyam00so大神的英文原创作品 Ember.js ObjectProxy isDestroyed Property。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。