简体   繁体   English

如何在我的Backbone视图中使用jQueryUI datepicker onSelect以及change事件?

[英]How can I use jQueryUI datepicker onSelect but also the change event in my Backbone view?

I've got a DateSelectorView that initializes jQuery UI datepicker, I've added the onSelect method which triggers a global event passing the date value. 我有一个DateSelectorView来初始化jQuery UI DateSelectorView ,我添加了onSelect方法,该方法触发一个传递日期值的全局事件。 I need the global event to fire but I also need access to the value from within my view, having the 'change input' event in my view though doesn't get acknowledged as DateSelectorView onSelect traps it, can anyone recommend how I can fix this? 我需要触发全局事件,但我还需要从我的视图中访问该值,尽管没有被确认为DateSelectorView onSelect捕获了它,但我的视图中还是有'change input'事件,有人可以建议我如何解决此问题? Is there a way of triggering the global event then letting the DateSelectorView onSelect continue to whatever other events need it? 有没有一种方法可以触发全局事件,然后让DateSelectorView onSelect继续执行其他任何需要的事件?

JS JS

App.Views.PersonView = Marionette.ItemView.extend({

    template: '.js-person-tmpl',

    events: {
        'change input': 'onSelect'
    },

    templateHelpers: function () {
        return {
            availableClasses: this.options.availableClasses.toJSON()
        }
    },

    initialize: function () {
        //console.log('PersonView::initialize', this.options, this.model.toJSON());
    },

    onRender: function() {
        this.dateSelector = new App.Views.DateSelectorView({
            el: this.$el.find('.js-date')
        });
    },

    onSelect: function() {
        console.log('date selected');

        // this.model.set('dateSelected', '');
    }
});

App.Views.DateSelectorView = Marionette.ItemView.extend({

    template: false,

    tagName: 'input',

    initialize: function() {
        //console.log('DateSelectorView::initialize');

        this.$el.datepicker({
            dateFormat: 'dd M yy',
            altFormat: 'yy-mm-dd',
            altField: '.js-date-hidden',
            showOtherMonths: true,
            selectOtherMonths: true,
            onSelect: function() {
                console.log('date:selected', this.value);

                Backbone.Events.trigger('date:selected', this.value);         
            }
        });
    }
});

JSFiddle: http://jsfiddle.net/kyllle/okmL8k8p/5/ JSFiddle: http : //jsfiddle.net/kyllle/okmL8k8p/5/

This can be fixed by Backbone events 这可以通过骨干事件来解决

App.Views.PersonView = Marionette.ItemView.extend({
   initialize: function () {
      App.events.on("doSomething_event", this.onSelect);
   },
   onSelect: function(date) {
      console.log(date);
   }
});

App.Views.DateSelectorView = Marionette.ItemView.extend({
     initialize: function() {
         this.$el.datepicker({
            dateFormat: 'dd M yy',
            altFormat: 'yy-mm-dd',
            altField: '.js-date-hidden',
            showOtherMonths: true,
            selectOtherMonths: true,
            onSelect: function() {
              App.events.trigger("doSomething_event", this.value);
            }
         });
     }
});

App.events = _.extend({}, Backbone.Events);

Hope this will helps you! 希望这对您有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM