Pregunta

In backbone view, I have following code in html

<input type="text" id="fromDate" name="fromDate"/><a id="cal">
<img src="img/calendar.gif"></a>

In view's js file, I have the following code:

define(['jquery', 'underscore', 'backbone', 'text!views/page1/page1.tpl'], function($, _, Backbone, tmpl_page1View) {

var page1View = Backbone.View.extend({
    // Setting the view's template property using the Underscore template method        
    template: _.template(tmpl_page1View),
    // View constructor
    initialize: function() {
        self = this;

    },
    // View Event Handlers
    events: {
        "click #page2": "clickedPage2",
        "click #cal":"calClicked"
    },
    // Renders the view's template to the UI
    render: function() {
        this.$el.html(this.template({data: this.templateData}));           
        // Maintains chainability
        return this;
    },
    clickedPage2:function(){
        window.location.href = "#page2"
    },
    calClicked:function(){
       $("#fromDate").datepicker({
          showOn: "button",
          buttonImage: "img/calendar.gif",
          buttonImageOnly: true
        });

    }

});
return page1View;
});

On click event of calendar icon, I want to open datepicker but it is not working. Can you help me on this one. Thank you.

¿Fue útil?

Solución

You should initialize datepicker e.g. in render method, and datepicker will open automatically on button click, so you won't need calClicked at all.

var page1View = Backbone.View.extend({
// Setting the view's template property using the Underscore template method        
template: _.template(tmpl_page1View),
// View constructor
initialize: function() {
    self = this;

},
// View Event Handlers
events: {
    "click #page2": "clickedPage2",
    "click #cal":"calClicked"
},
// Renders the view's template to the UI
render: function() {
    this.$el.html(this.template({data: this.templateData}));  

    // init datepicker
    this.$("#fromDate").datepicker({
      showOn: "button",
      buttonImage: "img/calendar.gif",
      buttonImageOnly: true
    });


    // Maintains chainability
    return this;
},
clickedPage2:function(){
    window.location.href = "#page2"
}    

});
return page1View;
});
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top