最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Access click event outside the el in backbone view - Stack Overflow

programmeradmin2浏览0评论

How can i access click event outside the el scope.

What i have : HTML :

<div class="right_btn"></div>
<div id="template_loader">
    <!-- HTML template goes here which contain form inputs-->
    <input type="text" class="forgot_password_email" name="forgot_password_email"/>
</div>

View :

var ForgotPasswordView = Backbone.View.extend({
    el: "#template_loader",
    initialize: function () {
        console.log('Forgot Password View Initialized');
    },
    render: function () {
        blockPage();
        var that = this;  

        $.get(App.baseUrl + 'templates/forgot-password-view.html', function (data) {
            template = _.template(data, { });
            that.$el.html(template);
            unblockPage();
        }, 'html'); 
    },
    events:{
        'click .right_btn':'forgotPasswordSubmit', //Doesn't fire as its outside of el
    }, 
    forgotPasswordSubmit: function(e){
        alert($(".forgot_password_email").val());
        e.preventDefault();
    }
});

Ive gone through the following :

Backbone.js views - binding event to element outside of "el"

Howto bind a click event in a Backbone subview

but doesn't really help me get it to work.

How can i get the click event of .right_btn inside the view. I cannot change the template structure to include the right_btn inside theel. Is here anyway to bind the outside element or recognize the click event inside a backbone view itself.

How can i access click event outside the el scope.

What i have : HTML :

<div class="right_btn"></div>
<div id="template_loader">
    <!-- HTML template goes here which contain form inputs-->
    <input type="text" class="forgot_password_email" name="forgot_password_email"/>
</div>

View :

var ForgotPasswordView = Backbone.View.extend({
    el: "#template_loader",
    initialize: function () {
        console.log('Forgot Password View Initialized');
    },
    render: function () {
        blockPage();
        var that = this;  

        $.get(App.baseUrl + 'templates/forgot-password-view.html', function (data) {
            template = _.template(data, { });
            that.$el.html(template);
            unblockPage();
        }, 'html'); 
    },
    events:{
        'click .right_btn':'forgotPasswordSubmit', //Doesn't fire as its outside of el
    }, 
    forgotPasswordSubmit: function(e){
        alert($(".forgot_password_email").val());
        e.preventDefault();
    }
});

Ive gone through the following :

Backbone.js views - binding event to element outside of "el"

Howto bind a click event in a Backbone subview

but doesn't really help me get it to work.

How can i get the click event of .right_btn inside the view. I cannot change the template structure to include the right_btn inside theel. Is here anyway to bind the outside element or recognize the click event inside a backbone view itself.

Share Improve this question edited May 23, 2017 at 12:23 CommunityBot 11 silver badge asked Nov 25, 2013 at 12:45 Roy M JRoy M J 6,9487 gold badges53 silver badges79 bronze badges 1
  • Possible duplicate of Backbone.js views - binding event to element outside of "el" – T J Commented Jan 30, 2016 at 4:16
Add a ment  | 

3 Answers 3

Reset to default 4

Unfortunately, there is no way of doing this using just the backbone library. Backbone expects the view to only handle the events within it's specific DOM element.

It would be best, if possible, to refactor your code so that you do not have to break these conventions. Blurring the boundary of a view can give you difficulty later on if you need to reposition the view and it has dependencies on it's parent environment.

If possible, create a parent view that contains the HTML you have listed above, and use that view to bind to the event and submit the form of it's child view.

If you have no choice but to do things this way, then I would advise using jQuery 'on' to bind to the event.

// Put this in your view to ensure that it is only bound when the form is
// added to the page. You could substitute 'on' for 'one' if you don't want
// the binding to maintain after the first submission.
var submitForm = function(){
  $('#template_loader form').submit();
};
$('.right_button').on('click', submitForm);

Actually there is a way :

The following is the original view which have a main template and inside the element template_loader, the forgotpassword template is renderer. The button right_btn is outside the elements scope and it did not fire.

var ForgotPasswordView = Backbone.View.extend({
    el: "#template_loader",
    initialize: function () {
        console.log('Forgot Password View Initialized');
    },
    render: function () {
        blockPage();
        var that = this;  

        $.get(App.baseUrl + 'templates/forgot-password-view.html', function (data) {
            template = _.template(data, { });
            that.$el.html(template);
            unblockPage();
        }, 'html'); 
    },
    events:{
        'click .right_btn':'forgotPasswordSubmit', //This will fire.
    }, 
    forgotPasswordSubmit: function(e){
        alert($(".forgot_password_email").val());
        e.preventDefault();
    }
});

So after a lot of research, i found a solution to my problem. Im not sure whether this has any drastic problems. I have not encountered anything yet and i will update this post if i find anything :

The idea to create a new instance for the same view, but this time with another el. By this way, we get to execute the initialize and render functions with the first el and use the next one for the events.

One downside is that you will not be able to use something like this to obtain first el peoperties. But since i am using jquery, i can use the selectors to get through this. Other than that, i did not find any issues... yet.!

new ForgotPasswordView ({
   el:'#master_template'
});

is your right_btn in another view?

you can do this by implementing a global event bus :)

see this answer for more details. Call a function in another Marionette.ItemView

发布评论

评论列表(0)

  1. 暂无评论