Last active
December 12, 2015 01:08
-
-
Save JogoShugh/4689117 to your computer and use it in GitHub Desktop.
Backbone Forms Model Binding
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- Seriously, this is it: --> | |
<html> | |
<body></body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@on "assetUpdated", (assetEditor, asset) -> | |
success "Save successful" | |
assetEditor._normalizeIdWithoutMoment asset | |
assetEditor._normalizeHrefWithoutMoment asset | |
assetEditor.listItemReplace asset |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"_links": { | |
"self": { | |
"href": "/platformtest/rest-1.v1/Data/Request/1150/1321", | |
"id": "Request:1150:1321" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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" | |
} | |
] | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Form */ | |
.bbf-form { | |
margin: 0; | |
padding: 0; | |
border: none; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(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); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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" | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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