|
|
|
|
|
by BillGoates
4896 days ago
|
|
Separating DOM and data I can understand, but I am not convinced Backbone is the right way to do it. My code would look more like: function postdata(url, data, callback) {
$.ajax({
url: '/status',
type: 'POST',
dataType: 'json',
data: data,
success: function (received) { callback(received); }
});
}
var statusform;
(function(pub) {
var status = ""
pub.init = init;
function init() {
$('#new-status form').submit(function(e) {
submitdata();
e.preventDefault();
});
}
function submitdata() {
var data = {
text: getfieldvalue("textarea")
};
if (!data.text) { return; }
postdata("/status", data, function(received) {
status = received.status;
drawscreen();
});
}
function getfieldvalue(fieldname) {
return $('#new-status').find(fieldname).val();
}
function drawscreen() {
if (status) {
$('#statuses').append('<li>' + status + '</li>');
$('#new-status').find('textarea').val('');
status = "";
}
}
}) (statusform);
$(document).onload(statusform.init);
Note 1: This code sample is untested, but should give a good enough idea what I am trying to do.
Note 2: For extra type safety, use typescript interfaces to model data. |
|