Created
February 28, 2012 11:13
-
-
Save jbrunton/1931953 to your computer and use it in GitHub Desktop.
DRY, MV* example
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
define([ | |
'core', | |
'util', | |
'viewmodels/geo/lat_lng' | |
], function(core, util, LatLng) { | |
var Destination = function(data) { | |
this.id = ko.observable(); | |
this.title = ko.observable(); | |
this.description = ko.observable(); | |
this.address = ko.observable(); | |
this.content = ko.observable(); | |
this.latLng = ko.observable(); | |
this.blogId = ko.observable(); | |
this.orderIndex = ko.observable(); | |
this.error = ko.observable(); | |
this.success = ko.observable(); | |
this.clientHref = ko.computed(function() { | |
return "/blogs/" + this.blogId() + "/destinations/" + this.id() + "/view"; | |
}, this); | |
this.editHref = ko.computed(function() { | |
return "/blogs/" + this.blogId() + "/destinations/" + this.id() + "/edit"; | |
}, this); | |
this.marker = null; | |
if (data) { | |
this.fromData(data); | |
} | |
}; | |
// mixin methods for ajax create, read, update and destroy requests | |
core.traits.compose(Destination, [ | |
util.CrudTrait("destinations") | |
]); | |
// serialization | |
Destination.prototype.toData = function() { | |
var data = { | |
title: this.title(), | |
description: this.description(), | |
address: this.address(), | |
content: this.content(), | |
lat_lng: this.latLng() | |
? this.latLng().toData() | |
: null, | |
blog_id: this.blogId(), | |
order_index: this.orderIndex() | |
}; | |
if (this.id()) { | |
data.id = this.id(); | |
} | |
return data; | |
}; | |
// deserialization | |
Destination.prototype.fromData = function(data) { | |
this.id(data.id); | |
this.title(data.title); | |
this.description(data.description); | |
this.address(data.address); | |
this.content(data.content); | |
if (data.lat_lng) { | |
this.latLng(new LatLng(data.lat_lng)); | |
} else { | |
this.latLng(null); | |
} | |
this.blogId(data.blog_id); | |
this.orderIndex(data.order_index); | |
return this; | |
}; | |
Destination.prototype.load = function(id, success) { | |
var self = this; | |
this.doReadReq( | |
function(data) { | |
self.fromData(data); | |
if (success) success(self); | |
}, | |
function() { | |
alert("There was an error contacting the server."); | |
}, | |
id | |
); | |
return this; | |
}; | |
return Destination; | |
}); |
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
<script type="text/x-jquery-tmpl" id="edit-dest-tmpl"> | |
<div class="content-left"> | |
<div class="modal modal-relative"> | |
<div class="modal-header"> | |
<h3 data-bind="text: blog.title" /> | |
<p>by <span data-bind="text: blog.author" /></p> | |
</div> | |
<div class="modal-body"> | |
<form class="form-horizontal"> | |
<fieldset class="control-group"> | |
<label for="title" class="control-label">Title</label> | |
<div class="controls"> | |
<input type="text" data-bind="value: destination.title" /> | |
</div> | |
</fieldset> | |
<fieldset class="control-group"> | |
<label for="description" class="control-label">Description</label> | |
<div class="controls"> | |
<input type="text" data-bind="value: destination.description" /> | |
</div> | |
</fieldset> | |
<fieldset class="control-group"> | |
<label for="address" class="control-label">Address</label> | |
<div class="controls"> | |
<input type="text" data-bind="value: destination.address" /> | |
</div> | |
</fieldset> | |
<fieldset class="control-group"> | |
<label for="content" class="control-label">Content</label> | |
<div class="controls"> | |
<textarea data-bind="value: destination.content" /> | |
</div> | |
</fieldset> | |
</form> | |
</div> | |
</div> | |
</div> | |
</script> |
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
<script type="text/x-jquery-tmpl" id="view-dest-tmpl"> | |
<div class="content-left"> | |
<div class="modal modal-relative"> | |
<div class="modal-header"> | |
<h3 data-bind="text: blog.title" /> | |
<p>by <span data-bind="text: blog.author" /></p> | |
<a class="nav-to" data-bind="attr: { href: blog.clientHref }">← back to blog</a> | |
</div> | |
<div class="modal-body"> | |
<h4 data-bind="text: destination.title" /> | |
<p class="help-block" data-bind="text: destination.address" /> | |
<hr /> | |
<em><p data-bind="text: destination.description" /></em> | |
<div data-bind="html: destination.content" /> | |
</div> | |
<div class="modal-footer"> | |
<a class="nav-to pull-right" data-bind="attr: { href: destination.editHref }">edit →</a> | |
</div> | |
</div> | |
</div> | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment