backbone-forms
backbone-forms copied to clipboard
Basic re-rendering doesn't work since v0.12
I'm finally getting around to upgrading my big app from the pre-refactor version of Backbone-Forms to the latest.
There's a common pattern that I used everywhere which apparently stopped working in v0.12... which is the ability to re-render a form and have it use the latest model data.
This test passes on v0.11 and fails on v0.12 through master.
test('re-render', function() {
var model = new Backbone.Model({ name: 'Foo' });
var form = new Form({
schema: { name: 'Text', },
model: model
});
var $el;
$el = form.render().$el;
equal($el.find('input[name=name]').val(), 'Foo'); // passes
model.set('name', 'Bar');
$el = form.render().$el;
equal($el.find('input[name=name]').val(), 'Bar'); // fails on v0.12+
});
Thoughts? Seems like a simple enough case.
#363 is related and also seems like a good idea (I always thought it was strange to not set this.value
in setValue
) --- however my case here is even simpler (updating the Model attributes and re-rendering a Form/View, which is a common a Backbone pattern)
I do this all the time and don't have any problems. My typical approach using Backbone:
class View extends Backbone.View
render: ->
super
@form.remove() if @form
@form = new Backbone.Form {@model}
@form.render()
@$el.append @form.el
Then whenever I update model data and re-render the view, the form will automatically get re-rendered.
This has worked under 0.13 and 0.14
@tamagokun what you're describing is not re-rendering a Backbone Form. It's destroying the form and re-creating a new one.
Ah, very true. I remember trying to call render()
again to see if re-rendering would work, but ended up just doing it as described above. It certainly isn't ideal, but the time needed to remove, re-create and render the form has not been noticeable in my experiences.
The idea of (recent) versions of Backbone-Forms is that a Form is just a regular view that could be extended, keep its own state, etc. so there are cases where you really need a re-render. Any thoughts on how this broke between v0.11 and v0.12 and if it can be easily fixed @powmedia ?
@philfreo: would #363 solve what you're doing? If not, what's the difference?
@gebrits I haven't tried it, but on first glance of the code I think #363 handles updates manually made calling setValue
on a form, whereas this issue is related to simply updating a model attribute (model.set()
) and expecting simply calling form.render()
again to show the new value.
But if my test above passes in that PR (I haven't tried it yet) then great, it may take care of both.
Are there any news on re-rendering not working? Here, re-rendering simply does nothing.