Skip to content

Instantly share code, notes, and snippets.

@green3g
Last active February 17, 2016 21:28
Show Gist options
  • Save green3g/f597e1987f78e48e79f3 to your computer and use it in GitHub Desktop.
Save green3g/f597e1987f78e48e79f3 to your computer and use it in GitHub Desktop.
{{#if objectsPromise.isPending}}
<p>Loading...</p>
{{/if}}
{{#if objectsPromise.isResolved}}
{{#each objects}}
{{#each .}}
{{#if renderField(%key)}}
<td>{{formatValue(.)}}</td>
{{/if}}
{{/each}}
</tr>
{{/each}}
{{/if}}
export const ViewModel = viewModel.extend({
define: {
objectModel: {
value: null
},
editable: {
type: 'boolean',
value: false
},
filter: {
type: '*',
value: null
},
group: {
type: 'string',
value: null
},
objectsPromise: {
get: function() {
return this.attr('objectModel').findAll({
filter: this.attr('filter') ? 'q=' + JSON.stringify(this.attr('filter')) : '',
group: this.attr('group') ? 'group=' + this.attr('group') : ''
});
}
},
objects: {
get: function(value, setAttr) {
this.attr('objectsPromise').then(setAttr);
}
},
formSubmit: function(scope, form, event) {
var data = can.$(form).serializeArray();
var edit_object = scope.attr('edit_object');
for (var i = 0; i < data.length; i++) {
var newData = data[i];
if (edit_object.attr(newData.name) !== newData.value) {
edit_object.attr(newData.name, newData.value);
}
}
console.log(edit_object.save());
scope.attr('edit_object', null);
//prevent the form from submitting
return false;
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment