hungfei
hungfei

Reputation: 17

Jquery Backbone.View.extend function call

i want to asking about what is directory.views.SearchPage for? can i call this function as above? is possibe to call page load?

directory.views.SearchPage = Backbone.View.extend({

templateLoader: directory.utils.templateLoader,
EmployeeListView: directory.views.EmployeeListView,

initialize: function() {
  this.template = _.template(this.templateLoader.get('search-page'));
},

render: function(eventName) {
  $(this.el).html(this.template(this.model.toJSON()));
  this.listView = new directory.views.EmployeeListView({el: $('ul', this.el), model:this.model});
  this.listView.render();
  return this;
},
events: {
  "keyup .search-key": "search"
},
// Start the search function
search: function(event) {
  var key = $('.search-key').val();
  this.model.findByName(key);
}
});

Upvotes: 0

Views: 228

Answers (1)

sixFingers
sixFingers

Reputation: 1295

From what I understand, you want to initialize, render and then call the "share" function on your main view class on page load. Since jQuery is available, i would go simply with:

var searchPage = new directory.views.SearchPage();
$(document).ready(function(e) {
    searchPage.render();
    searchPage.search(e);
});

But please note that:

  • I don't see a reason to pass the event argument from $(document).ready, or at least it's not used at all in your code
  • my answer is pretty generic, since the question is generic (it gets down to "how I call a function on page load?")
  • What you define using Backbone.View.extend is somewhat similar to a classic prototype (not really, but gives you an idea). Therefore, you should then instantiate a new instance from this extended "class" before accessing its methods and properties.

Upvotes: 2

Related Questions