문제

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.

도움이 되었습니까?

해결책

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;
});
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top