backbone.stickit icon indicating copy to clipboard operation
backbone.stickit copied to clipboard

Support jquery element as selector in addBinding method

Open guroot opened this issue 11 years ago • 9 comments

guroot avatar Mar 07 '15 20:03 guroot

What's the use case for this? Why would you ever need to pass your own elements?

akre54 avatar Mar 09 '15 22:03 akre54

An object could be created dynamically and binded later. I often create objects with backbone view at runtime.

var element1 = 
Backbone.view.extend({
tagName : 'input',
....
render : function(){
return this;
}
)}

return Backbone.View.extend({
render : function(){
this.element1 = new element1;
this.$el.append(this.element1.render().el);
this.addBinding(null, this.element1, 'author');
return this;
}

You got the idea..

guroot avatar Mar 09 '15 22:03 guroot

I'm not sure I see this as useful. Why aren't you using the normal bindings api?

Also, when would element1 ever need to be selected independently from its parent view? Why is a selector string not good enough here?

akre54 avatar Mar 09 '15 22:03 akre54

You should really be building views like this:

var InputView = Backbone.View.extend({
  tagName: 'input'
  ...
});

var FormView = Backbone.View.extend({
  tagName: 'form',
  bindings: {
    'input': 'author'
  },
  render: function() {
    this.$el.append((new InputView()).render().$el);
    this.stickit();
    return this;
  }
});

akre54 avatar Mar 09 '15 22:03 akre54

Input element could have more complex code which make it easier to be itself a View. For example, if it contain auto-complete feature with his own collection and model.

guroot avatar Mar 09 '15 22:03 guroot

When a form is complex, I prefer sometime have multiple views rather that a big one.

guroot avatar Mar 09 '15 22:03 guroot

When a form is complex, I prefer sometime have multiple views rather that a big one.

Yes, absolutely. There's nothing stopping you from passing models around in your view (new InputView({model: this.model}) or new InputView({model: this.authorModel})). In fact, that's recommended. But you really shouldn't be passing around arbitrary elements like this.

Views should almost exclusively care only about their child elements, not sibling elements. If you allow passing arbitrary elements around you break this concept. If you only care about child elements (as selector strings do currently) than this isn't a concern.

akre54 avatar Mar 09 '15 22:03 akre54

Ok I understand what you mean. I though it could be convenient to add this but maybe I'm wrong.

My personnal project is a dynamic forms generator who create fields from parameters passed to the view. They are dynamically generated and linked to the model. Maybe I should append them before calling stickbit. I actually generate them, bind them.. and finally, when rendering, append them.

Excuse my english, not my native language as you probably imagine.

guroot avatar Mar 09 '15 23:03 guroot

No worries! Hopefully I understood your point.

I'm not sure Stickit is the best technology for making an arbitrary form creator, but if your form creator must be dynamic, you could do some variation of your code above, just with a string selector instead of passing the element (or even better, give each subview its own bindings).

I'll have a think about a way to possibly add this feature in the future, that hopefully won't break addBinding's contract too much.

akre54 avatar Mar 09 '15 23:03 akre54