SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Guru
    Join Date
    Jun 2009
    Posts
    815
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    backbone.js -- testing for whether a field in a form is empty..

    I can't figure out how to detect if a form in a field is empty with backbone....

    function to submit I was given:

    Code:
    submit: function () {
    	// set values from form on model
    	this.model.set({
    		author: this.$el.find('.author').val(),
    		text: this.$el.find('.text').val()
    	});
    
              //  I added these two lines:
    		alert(this.author);  // ***** returns 'undefined' even if it's not empty......
    	  if (this.author === '') alert('error');   //   ****** not working....
    	
    	// set an id if model was a new instance
    	// note: this is usually done automatically when items are stored in an API
    	if (this.model.isNew()) {
    		this.model.id = Math.floor(Math.random() * 1000);
    	}
    	
    	// trigger the 'success' event on form, with the returned model as the only parameter
    	this.trigger('success', this.model);
    	
    	// remove form view from DOM and memory
         	this.remove(); 
    	return false;
    },

    I searched for "backbone test if form field is empty" -- didn't find answer... how complicated can this be??

    why is this not working.....
    if (this.author === '')


    thank you...

  2. #2
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,189
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    The property author will always return undefined as you need to call upon the model in order to get the data back from it, E.g.

    Code JavaScript:
    if (!this.model.get('author').length) {
        // Error!
    }

  3. #3
    SitePoint Guru
    Join Date
    Jun 2009
    Posts
    815
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    thank you!! :-)


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •