Skip to content

Instantly share code, notes, and snippets.

@Sinled
Created March 20, 2017 15:11
Show Gist options
  • Save Sinled/af89f2519d956f4eacf9665277b91908 to your computer and use it in GitHub Desktop.
Save Sinled/af89f2519d956f4eacf9665277b91908 to your computer and use it in GitHub Desktop.
check-double-save
import DS from 'ember-data';
export default DS.JSONAPIAdapter.extend({
createRecord(...params) {
alert('createRecord');
return Ember.RSVP.Promise.resolve({
data: {
type: 'tournament',
id: Date.now(),
attributes: {
title: 'updated world' + (new Date)
}
}
})
}
});
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle',
actions: {
saveTournament(tournament) {
alert('save');
tournament.save();
}
}
});
import Model from 'ember-data/model';
import Ember from 'ember';
const { inject, computed, isEmpty, RSVP: { Promise } } = Ember;
export function initialize(/* application */) {
Model.reopen({
save() {
alert('redefined save');
if (this.get('validations.isInvalid')) {
return Promise.reject('Falid to save');
}
return this._super();
}
});
}
export default {
name: 'model-save-extend',
initialize
};
import Model from "ember-data/model";
import attr from "ember-data/attr";
import { belongsTo, hasMany } from "ember-data/relationships";
import { validator, buildValidations } from 'ember-cp-validations';
const Validations = buildValidations({
title: validator('presence', true),
});
export default Model.extend(Validations, {
title: attr('string')
});
import Ember from 'ember';
export default Ember.Route.extend({
model() {
return this.store.createRecord('tournament', { title: 'hello world' });
}
});
<h1>Welcome to {{appName}}</h1>
<br>
<br>
{{outlet}}
<br>
<h2>Tournament</h2>
{{model.title}}
<hr>
is valid: {{model.validations.isValid}}
<br>
<button {{action 'saveTournament' model}} type='button'>save tournament</button>
{
"version": "0.11.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.11.0",
"ember-data": "2.11.0",
"ember-template-compiler": "2.11.0",
"ember-testing": "2.11.0"
},
"addons": {
"ember-cp-validations": "3.2.3"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment