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


Embeer.js RouterService setProperties()用法及代码示例


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

setProperties() 方法用于更改多个属性的值。

用法:

setProperties(hash)

参数:

  • hash: 键的哈希值以及它们需要更改为的值。

返回值:传递的哈希对象。

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

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

npm install ember-cli

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

ember new <project-name> --lang en

要启动服务器,请键入:

ember serve

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

ember generate route students

应用程序/路线/students.js

Javascript


import Route from '@ember/routing/route'; 
import EmberObject from '@ember/object'; 
  
const Student = EmberObject.extend({ 
}); 
  
export default class StudentsRoute extends Route { 
    student; 
    createStudent() { 
        let student = Student.create({ 
            name: 'Alix Mainston', 
            gender: 'F', 
            class: 9, 
            grade: 'B', 
            marks: 98, 
            pocket_money: 9643, 
        }); 
        return student; 
    } 
    changeDetails(student) { 
        student.setProperties({ 
            pocket_money: 10000, 
            gender: 'M'
        }) 
    } 
    model() { 
        this.student = this.createStudent(); 
        this.changeDetails(this.student) 
        return this.student; 
    } 
    setupController(controller, model) { 
        super.setupController(controller, model); 
        controller.set('student', this.student); 
    } 
} 

应用程序/模板/students.hbs

HTML


{{page-title "Students"}} 
<h1>Details:</h1> 
<div>Name: {{this.student.name}}</div> 
<div>Gender: {{this.student.gender}}</div> 
<div>Class: {{this.class}}</div> 
<div>Grade: {{this.student.grade}}</div> 
<div>Marks: {{this.student.marks}}</div> 
<div>Pocket Money: {{this.student.pocket_money}}</div> 
{{outlet}}

输出:

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

ember generate route details

应用程序/路线/details.js

Javascript


import Route from '@ember/routing/route'; 
import EmberObject from '@ember/object'; 
  
const Person = EmberObject.extend({}); 
  
export default class DetailsRoute extends Route { 
    person; 
    createPerson() { 
        let person = Person.create({ 
            name: 'Anubhav', 
            mobile: '1298119967', 
            age: 20, 
            salary: 10000, 
            city: 'Patna', 
            country: 'India', 
            gender: 'M', 
            zipCode: '800020', 
        }); 
        return person; 
    } 
    changeDetails(person) { 
        let newAge = 51; 
        let newCountry = 'England'; 
        let update = { 
            age: newAge, 
            country: newCountry, 
        } 
        person.setProperties(update); 
    } 
    model() { 
        this.person = this.createPerson(); 
        this.changeDetails(this.person); 
        return this.person; 
    } 
    setupController(controller, model) { 
        super.setupController(controller, model); 
        controller.set('person', this.person); 
    } 
} 

应用程序/模板/details.hbs

HTML


{{page-title "Details"}} 
<h1>Person</h1> 
<div>Name: {{this.person.name}}</div> 
<div>Age: {{this.person.age}}</div> 
<div>Salary: {{this.person.salary}}</div> 
<div>Gender: {{this.person.gender}}</div> 
<div>Zip Code: {{this.person.zipCode}}</div> 
<div>Country: {{this.person.country}}</div> 
<div>Mobile: {{this.person.mobile}}</div> 
{{outlet}}

输出:

参考:https://api.emberjs.com/ember/4.7/classes/RouterService/methods



相关用法


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