Skip to content

Commit

Permalink
Merge branch 'initial_sync'
Browse files Browse the repository at this point in the history
  • Loading branch information
Yuri Gadow committed Jan 24, 2012
2 parents d52b82b + 5fb2b53 commit 4292aaa
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions vendor/assets/javascripts/backbone_datalink.js
Expand Up @@ -4,16 +4,16 @@
return $.extend($.fn, {
backboneLink: function (model) {
return $(this).find(":input").each(function () {
var el, name;
var el, name, copyFormToModel, copyModelToForm;

el = $(this);
name = el.attr("name");

if (_.isUndefined(name) || _.isEmpty(name)) {
return;
}
model.bind("change:" + name, function () {

copyModelToForm = function () {
var modelValue;

modelValue = model.get(name);
Expand All @@ -24,13 +24,13 @@
el.removeAttr("checked");
}
} else {
el.val(modelValue);
(modelValue === null) ? el.val("") : el.val(modelValue);
}

return el;
});
};

return $(this).bind("change", function () {
copyFormToModel = function () {
var attrs, checked, el, modelValue;
attrs = {};
el = $(this);
Expand All @@ -53,11 +53,17 @@
}

} else {
attrs[name] = el.val();
attrs[name] = (/^(\s*|null)$/.test(el.val())) ? null : el.val();
}

return model.set(attrs);
});
};

copyModelToForm();
model.bind("change:" + name, copyModelToForm);
el.bind("change", copyFormToModel);

return el;
});
}
});
Expand Down

0 comments on commit 4292aaa

Please sign in to comment.