2

I have a general question regards where I should do view element addin initialzation. Say for instance I have a typeahead plugin that requires initialization like so.

$(element).typeahead();

Using Backbone.Marionette where is the best place todo this type of view specific code? My original thought was to override the onRender() method in my view class and do this specifically everytime for elements I know about at design time. However, I would like a more general approach like monitoring the newly added dom elements and checking if element has an identifier like 'data-typeahead' and then automatically initialising it. However, I don't know any jQuery function that would do this? Or even if this is a realistic idea in terms of the overhead of dom parsing especially on mobile devices?

Any thoughts / ideas on how to solve this or where to best place to do this sort of code would be awesome!

Thanks

Jon

Jon
  • 4,295
  • 6
  • 47
  • 56
  • 3
    I think it would be a better idea to add the initialization of typeahead in the views onShow event, but have a look on these threads http://stackoverflow.com/questions/1091661/ http://stackoverflow.com/questions/3233991/ http://stackoverflow.com/questions/648996/ if you want more information on how one could implement something that listens for DOM changes, but there's lots of warnings that it's slow and a comment from this year (2013) points to a statement that the mutation event module is deprecated. – Trond Hatlen May 19 '13 at 18:36
  • Yeah I thought it would be to performance heavy to monitor the add but the onShow function is a good idea. Thanks. – Jon May 19 '13 at 18:54
  • You're welcome, that was a fun thing to look into. – Trond Hatlen May 19 '13 at 19:10
  • you can also listen to "dom:refresh" (Marionette v1.0.2) event in your view to attach plugins – deven98602 May 20 '13 at 11:36

1 Answers1

1

For those interested I used @Trond suggestions and implemented view init statements in the onShow method.

Jon
  • 4,295
  • 6
  • 47
  • 56