Skip to content

Instantly share code, notes, and snippets.

@Subtletree
Created June 14, 2017 06:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Subtletree/b3fc1a10ec8c8974224629f9a44a319b to your computer and use it in GitHub Desktop.
Save Subtletree/b3fc1a10ec8c8974224629f9a44a319b to your computer and use it in GitHub Desktop.
New Twiddle
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle',
actions: {
createComment() {
let post = this.get('model');
let comment = this.get('store').createRecord('comment', {post});
post.set('comment', comment);
},
deleteComment() {
let post = this.get('model');
post.get('comment').then(comment => {
comment.deleteRecord();
});
}
}
});
import Model from "ember-data/model";
import attr from "ember-data/attr";
import { belongsTo, hasMany } from "ember-data/relationships";
export default Model.extend({
post: belongsTo('post')
});
import Model from "ember-data/model";
import attr from "ember-data/attr";
import { belongsTo, hasMany } from "ember-data/relationships";
export default Model.extend({
comment: belongsTo('comment'),
// false positive
hasComment1: Ember.computed('comment', function() {
return Ember.isPresent(this.get('comment'));
}),
// Doesn't work for non persisted comments
hasComment2: Ember.computed('comment.id', function() {
return Ember.isPresent(this.get('comment.id'));
}),
// uses 'private' content
hasComment3: Ember.computed('comment', function() {
return Ember.isPresent(this.get('comment.content'));
}),
hasComment4: Ember.computed('comment', function() {
return Ember.isPresent(this.belongsTo('comment'));
})
});
import Ember from 'ember';
export default Ember.Route.extend({
model() {
return this.get('store').createRecord('post');
}
});
<h1>Welcome to {{appName}}</h1>
<br>
<br>
{{outlet}}
<br>
<br>
<button {{action 'createComment'}}>Create comment</button>
<button {{action 'deleteComment'}}>Delete comment</button>
<p>model.hasComment1: {{model.hasComment1}}</p>
<p>model.hasComment2: {{model.hasComment2}}</p>
<p>model.hasComment3: {{model.hasComment3}}</p>
<p>model.hasComment4: {{model.hasComment4}}</p>
{
"version": "0.12.1",
"EmberENV": {
"FEATURES": {}
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js",
"ember": "2.12.0",
"ember-template-compiler": "2.12.0",
"ember-testing": "2.12.0"
},
"addons": {
"ember-data": "2.12.1"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment