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


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


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

ArrayProxy的removeObserver()方法是用於移除對象的已注冊觀察者的方法。此方法斷開 key 和觀察者的連接。

用法:

this.object.removeObserver( key, target, method, sync );

參數:

  • key: 這是觀察者將從中移除的。
  • target: 它是一個對象,是 key 的所有者。
  • method: 它是根據觀察者命令調用的。
  • sync: 它指定觀察者是否異步。

返回類型:可觀察到的。

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

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

npm install ember-cli

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

ember new <project-name> --lang en

要啟動服務器,請鍵入:

ember serve

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

ember generate route test1

應用程序/路線/test1.js

Javascript


import Route from '@ember/routing/route';
import ArrayProxy from '@ember/object';
import { computed } from '@ember/object';
import Ember from 'ember';
const Student = ArrayProxy.extend({
    Changed: Ember.observer('marks', function () {
        console.log(`Marks of ${this.name} changed`);
    }),
});
export default class StudentsRoute extends Route {
    student = Student.create({
        name: 'Arun Verma',
        class: 12,
        Branch: 'Commerce',
        gender: 'M',
        marks: 73,
        mobile: '837483783',
        Roll_No: '1238372',
    })
    model() {
        return this.student;
    }
    setupController(controller, model) {
        super.setupController(controller, model);
        controller.set('student', this.student);
    }
}

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

Javascript


import Ember from 'ember';
import EmberObject from '@ember/object';
import { cacheFor, destroy } from '@ember/object';
export default Ember.Controller.extend({
    actions: {
        del() {
            let temp = this.student.removeObserver('marks', this.student,
                'Changed');
            console.log('Observer removed!!')
        },
        Change() {
            this.student.set('marks', this.student.get('marks') + 10);
        },
    }
});

應用程序/模板/test1.hbs

HTML


{{page-title "ArrayProxy"}}
<table>
    <h1>Student's Details:</h1>
    <div><b>Name:</b> {{this.student.name}}</div>
    <div><b>Gender:</b> {{this.student.gender}}</div>
    <div><b>Class:</b> {{this.student.class}}</div>
    <div><b>Mobile:</b> {{this.student.mobile}}</div>
    <div><b>Grade:</b> {{this.student.Grade}}</div>
    <div><b>Marks:</b> {{this.student.marks}}</div>
    <div><b>Branch:</b> {{this.student.Branch}}</div>
     
</table>
<br /><br/>
<input
    type="button"
    id="remove"
    value="Remove Observer"
    {{action 'del'}}
/><br /><br/>
<input
    type="button"
    id="Change"
    value="Change The Marks"
    {{action 'Change'}}
/>
{{outlet}}

輸出:

刪除觀察者輸出1

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

ember generate route test2

應用程序/路線/test2.js

Javascript


import Route from '@ember/routing/route';
import { computed } from '@ember/object';
import { tracked } from '@glimmer/tracking';
import Ember from 'ember';
import ArrayProxy from '@ember/object/proxy';
const Employee = ArrayProxy.extend({
    fullName: computed('firstName', 'lastName', function () {
        return this.get('firstName') + ' ' +
            this.get('lastName');
    }),
    change: Ember.observer('age', function () {
        console.log(`Age is changed`);
    }),
});
export default class DetailsRoute extends Route {
    value = 34;
    @tracked
    person = Employee.create({
        firstName: 'Rohil',
        lastName: 'Sahu',
        mobile: '928393839',
        age: 32,
        language: 'Hindi',
        country: 'India',
        gender: 'M',
        zipCode: '9384983',
        salary: 120000,
    });
    model() {
        return this.person
    }
    setupController(controller, model) {
        super.setupController(controller, model);
        controller.set('person', this.person);
        controller.set('value', this.value);
    }
}

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

Javascript


import Ember from 'ember';
import EmberObject from '@ember/object';
export default Ember.Controller.extend({
    actions: {
        change(data) {
            this.person.set('age', data);
        },
        remove() {
            this.person.removeObserver('age', this.person, 'change');
            console.log('Observer Removed!!!')
        }
    }
});

應用程序/模板/test2.hbs

HTML


{{page-title "ArrayProxy"}}
<table>
    <h1>Employee's Details: </h1>
    <div><b>Name:</b> {{this.person.fullName}}</div>
    <div><b>Gender:</b> {{this.person.gender}}</div>
    <div><b>Country:</b> {{this.person.country}}</div>
    <div><b>Salary:</b> {{this.person.salary}}</div>
    <div><b>Zip Code:</b> {{this.person.zipCode}}</div>
    <div><b>Language Speaks:</b>{{this.person.language}}</div>
    <div><b>Mobile No.:</b>{{this.person.mobile}}</div>
    <div><b>Age:</b> {{this.person.age}}</div>
</table>
<br />
{{input value=this.value}}
<br/>
<input
    type="button"
    id="Change"
    value="Change Age"
    {{action 'change'this.value}}
><br/><br/>
<input
    type="button"
    id="remove"
    value="Remove Observer"
    {{action 'remove'}}
>
{{outlet}}

輸出:

刪除觀察者輸出2

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



相關用法


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