Skip to content

Instantly share code, notes, and snippets.

@JogoShugh
Last active December 12, 2015 01:08
Show Gist options
  • Save JogoShugh/4689117 to your computer and use it in GitHub Desktop.
Save JogoShugh/4689117 to your computer and use it in GitHub Desktop.
Backbone Forms Model Binding
<!-- Seriously, this is it: -->
<html>
<body></body>
</html>
@on "assetUpdated", (assetEditor, asset) ->
success "Save successful"
assetEditor._normalizeIdWithoutMoment asset
assetEditor._normalizeHrefWithoutMoment asset
assetEditor.listItemReplace asset
createAsset: (assetName, callback) ->
url = @getAssetUrl(assetName)
@saveAsset url, "assetCreated", callback
updateAsset: (href, callback) ->
url = @host + href + "?" + $.param(@queryOpts)
@saveAsset url, "assetUpdated", callback
saveAsset: (url, eventType, callback) ->
validations = @form.validate()
if validations?
error "Please review the form for errors"
return
dto = @createDto()
debug "Dto:"
debug dto
request = @createRequest(
url: url
type: "POST"
data: JSON.stringify(dto)
contentType: @contentType
)
$.ajax(request).done((data) =>
@trigger eventType, @, data
callback data if callback
).fail @_ajaxFail
"_links": {
"self": {
"href": "/platformtest/rest-1.v1/Data/Request/1150/1321",
"id": "Request:1150:1321"
}
{
"RequestedBy": "Blaine Stussy",
"Name": "Add the Custom_Team custom field to the Request form",
"Description": "Backbone.Events is Awesome!",
"_links": {
"self": {
"href": "/platformtest/rest-1.v1/Data/Request/1150/1322",
"id": "Request:1150:1322"
},
"Scope": [
{
"href": "/platformtest/rest-1.v1/Data/Scope/0",
"idref": "Scope:0"
}
],
"Priority": [
{
"href": "/platformtest/rest-1.v1/Data/RequestPriority/169",
"idref": "RequestPriority:169"
}
]
}
}
/* Form */
.bbf-form {
margin: 0;
padding: 0;
border: none;
}
$(function () {
var User = Backbone.Model.extend({
schema: {
title: {
type: 'Select',
options: ['', 'Mr', 'Mrs', 'Ms']
},
name: 'Text',
email: {
validators: ['required', 'email']
},
birthday: 'Date',
password: 'Password',
notes: {
type: 'List',
listType: 'Text'
}
}
});
var user = new User({
title: 'Mr',
name: 'Sterling Archer',
email: 'sterling@isis.com',
birthday: new Date(1978, 6, 12),
password: 'dangerzone',
notes: [
'Buy new turtleneck',
'Call Woodhouse',
'Buy booze']
});
var form = new Backbone.Form({
model: user
}).render();
$('body').append(form.el);
});
default :
RequestedBy:
title: 'Requested By'
autofocus: true
Name:
title: 'Request Title'
Description:
title: 'Request Description (Project & Why needed)'
type: 'TextArea'
optional: true
Priority:
title: 'Priority'
type: 'Select'
assetName: 'RequestPriority'
{
"RequestedBy": "Blaine Stussy",
"Name": "Add the Custom_Team custom field to the Request form",
"Description": "Please add the Custom_Team field, between the Description and Priority fields, to the Request form for the CapEx project, and only for the CapEx project",
"Priority": "RequestPriority:167"
}
{
"RequestedBy": "Your Name",
"Name": "Add the Custom_Team custom field to the Request form",
"Description": "Please add the Custom_Team field, between the Description and Priority fields, to the Request form for the CapEx project, and only for the CapEx project",
"_links": {
"Scope": {
"idref": "Scope:0"
},
"Priority": {
"idref": "RequestPriority:169"
}
}
}
createDto: ->
dto = @form.getValue()
# TODO: hard-coded for test
dto._links = Scope:
idref: @projectIdref
$("#fields select").each ->
el = $(this)
id = el.attr("name")
val = el.val()
relationAssetName = el.attr("data-rel")
if relationAssetName
dto._links[relationAssetName] = idref: val
delete dto[id]
return dto
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment