Note that there are some explanatory texts on larger screens.

plurals
  1. POBackbone views can't access jQuery selectors
    primarykey
    data
    text
    <p>I have a Backbone View which handles a registration component, because this required a lot of form access I had the idea to store the form selectors in an object property.</p> <pre><code>define(["models/security/user", 'text!templates/security/registration.html'], function(SecurityUserModel, Template){ var SecurityRegistrationView; SecurityRegistrationView = Backbone.View.extend({ initialize: function(){ this.model = new SecurityUserModel(); this.model.bind("validated:valid", this.valid); this.model.bind("validated:invalid", this.invalid); Backbone.Validation.bind(this); this.render(); }, render: function(){ $(this.el).append(Template); }, events: { "submit form": "submit" }, form: { "username": $("#_user_username") , "email": $("#_user_email") , "password": $("#_user_password") }, submit: function(e){ e.preventDefault(); this.model.set("username", this.form.username.val()); this.model.set("password", this.form.email.val()); this.model.set("email", this.form.password.val()); this.model.validate(); // debug console.log([this.form, this.form.username.val(), this.form.email.val()]); if (this.model.isValid) { this.model.save(); } }, valid: function(model, attrs){ // error this.form.attrs[0].username.parent("div.control-group").addClass("success"); }, invalid: function(model, attrs){ // error this.form.attrs[0].username.parent("div.control-group").addClass("error"); } }); return SecurityRegistrationView; }); </code></pre> <p>When I console.log this.form, I get an object which looks quite good nothing undefined or weird:</p> <pre><code>[ Object email: e.fn.e.init[0] context: HTMLDocument selector: "#_user_email" __proto__: Object[0] password: e.fn.e.init[0] context: HTMLDocument selector: "#_user_password" __proto__: Object[0] username: e.fn.e.init[0] context: HTMLDocument selector: "#_user_username" __proto__: Object[0] __proto__: Object ] </code></pre> <p>Loggin an jquery function accessing the selectors (this.form.username.val()) fails with undefined.</p> <p>Why can't use jQuery functions on properties ($el uses this to) where is the thinking error?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload