Wrapping meteor.js template templates in coffeescript classes

I am completely digging a meteor, but I am stuck trying to reduce the global nature of the examples and add the OOP trait.

Currently, my code looks like this:

# View for Search Form form = Template.SearchForm form.events = 'submit #search_form' : query_submitted 'click #load_more' : -> Songs.get_next_page() 'focus #query' : clear_query_field form.page = -> Songs.page form.total_pages = -> Songs.total_pages 

But, the spine or spine, what I really would like to have looks something like this:

 class SearchForm extends Template.SearchForm events: 'submit #search_form' : query_submitted 'click #load_more' : -> Songs.get_next_page() 'focus #query' : clear_query_field page : -> Songs.page total_pages : -> Songs.page # etc etc form = new SearchForm 

What is the right way to wrap a rudder pattern in a meteor?

I managed to wrap Meteor.Collection, but since handlebars names the object after the template, I'm not sure how to do it for the template.

UPDATED

@greg indicated that you can use _.extend to add properties. This works, but what if I want to dump the query_submitted and clear_query_field event handler methods into a class? Something like that:

 _.extend Template.SearchForm, events : 'submit #search_form' : @query_submitted 'click #load_more' : -> Songs.get_next_page() 'focus #query' : @clear_query_field page : -> Songs.page total_pages : -> Songs.total_pages clear_query_field : (event) -> console.log 'focus' query_submitted : (event) -> event.preventDefault() Songs.clear() Songs.query = $('#query')[0].value Songs.search() 

Does not work. Event handlers are not called properly, and I get errors in the console, for example:

Uncaught TypeError: Object [object Window] does not have a 'Query_submitted' method

Similarly

 events : 'submit #search_form' : (e) -> @query_submitted(e) 

gives:

Uncaught TypeError: cannot call the 'call' method from undefined

So what is missing?

+6
source share
2 answers

The meteor comes with an underline so you can:

 _.extend Template.SearchForm, events: 'submit #search_form' : query_submitted 'click #load_more' : -> Songs.get_next_page() 'focus #query' : clear_query_field page: -> Songs.page total_pages: -> Songs.page 
+2
source

You tried to replace @ with Template.Searchform. in your event bindings?

+1
source

Source: https://habr.com/ru/post/916014/


All Articles