Skip to content

Instantly share code, notes, and snippets.

@mixonic
Created May 15, 2012 18:34
Show Gist options
  • Save mixonic/2704020 to your computer and use it in GitHub Desktop.
Save mixonic/2704020 to your computer and use it in GitHub Desktop.
<form>
{{#if displayingErrors}}
<div id="error_explanation" class="clearfix">
<h5>An error stopped your settings from being saved:</h5>
<ul>
{{#each content.errors}}
<li>{{ this }}</li>
{{/each}}
</ul>
</div>
<div>
{{/if}}
<!-- Some Ember.TextFields and stuff -->
<button type="submit" style="float:left" class="green" {{bindAttr disabled="buttonDisabled"}}>{{#if content.isSaving}}Working...{{else}}Update Settings{{/if}}</button>
<div class="message"></div>
</form>
class SettingsController < ApplicationController
def update
if current_user.settings.update_attributes(params[:settings])
render layout: nil, json: { } // Your object should go back as json
else
render status: 422, layout: nil, json: { errors: ['There was an unknown error updating your settings. Please contact support if the issue persists.'] } // Errors go back as 422
end
end
end
// The error handling for rest objects is from a ember-data pull request:
//
// https://github.com/emberjs/data/pull/228
//
// Merged in my branch as well:
//
// https://github.com/mixonic/data/tree/spinto
//
App.EditSettingsView = Ember.View.extend({
templateName: 'ember/templates/editing_settings',
buttonDisabledBinding: "content.isSaving",
displayingErrors: false,
submit: function(event) {
event.preventDefault();
App.store.commit();
},
setDisplayingErrors: function(){
if (!this.getPath('content.isValid')) {
this.set('displayingErrors', true);
} else if (!this.getPath('content.isDirty')) {
this.set('displayingErrors', false);
}
}.observes('content.isValid','content.isDirty'),
setSuccessMessage: function(){
if (this.getPath('content.isSaving')) {
this.set('saving', true);
} else if (this.get('saving')) {
this.set('saving', false);
this.$('.message').delay(1).html('Your settings have been saved, and the changes are live.').fadeIn(200).delay(2000).fadeOut(200);
}
}.observes('content.isSaving')
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment