Skip to content

Instantly share code, notes, and snippets.

@James-Byrne
Last active August 24, 2019 09:14
Show Gist options
  • Save James-Byrne/f0f31f50032afcf9409330b790ae54af to your computer and use it in GitHub Desktop.
Save James-Byrne/f0f31f50032afcf9409330b790ae54af to your computer and use it in GitHub Desktop.
Testing defineProperty
import Ember from 'ember';
import { defineProperty, computed } from '@ember/object';
export default Ember.Controller.extend({
appName: 'Ember Twiddle',
init() {
const attrs = { firstName: 'Foo', lastName: 'Bar' };
const model = this.store.createRecord('my-model', attrs);
defineProperty(model, 'fullName', computed('model.{firstName,lastName}', {
get: () => {
const { firstName, lastName } = this.model;
return `${firstName} ${lastName}`;
},
set: (key, value) => {
const names = value.split(' ');
this.set('model.firstName', names[0]);
this.set('model.lastName', names[1]);
return value;
},
}));
this.set('model', model);
},
actions: {
onSubmit(fullName) {
this.set('model.fullName', fullName);
},
}
});
import Model from 'ember-data/model';
import attr from 'ember-data/attr';
import { belongsTo, hasMany } from 'ember-data/relationships';
export default Model.extend({
firstName: attr('string'),
lastName: attr('string'),
});
<h1>Welcome to {{appName}}</h1>
<br>
<br>
{{outlet}}
<br>
<br>
<h4>First Name: {{model.firstName}}</h4>
<h4>Last Name: {{model.lastName}}</h4>
<h4>Full Name: {{model.fullName}}</h4>
<p>Please enter a new first and last name</p>
<input value={{newValue}} onchange={{action (mut newValue) value="target.value"}}/>
<button onclick={{action "onSubmit" newValue}}>Submit</button>
{
"version": "0.15.1",
"EmberENV": {
"FEATURES": {}
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js",
"ember": "3.4.3",
"ember-template-compiler": "3.4.3",
"ember-testing": "3.4.3"
},
"addons": {
"ember-data": "3.4.2"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment