Jump to content

Backbone newbie question


Go to solution Solved by CtnChainsaw,

Recommended Posts

Hi all,

 

I'm new to backbone and JQuery and I'm trying to get my head around it.  I'm doing exercise 2 from this book:

 

 

http://addyosmani.github.io/backbone-fundamentals/#getting-set-up

 

 

It says "You may notice that the file input for the cover image isn’t working, but that is left as an exercise to the reader."

I've tried uploading an image and I get this in the console:

 

GET file:///C:/fakepath/dan_brown.jpg

 

 

This should be easy enough to do.

 

Anyone got any ideas?

 

 

Thank you

Link to comment
https://forums.phpfreaks.com/topic/279636-backbone-newbie-question/
Share on other sites

Something like this will do it.  However all elements then have 'img/' prepended to the content because the first if statement evaluates to true each time.

 

Anybody got any tips to sort this?

	addBook: function( e ) {
		e.preventDefault();

		var formData = {};

		$( '#addBook div' ).children( 'input' ).each( function( i, el ) {
			if( $( el ).val() != '' )
			{
				if($("#coverImage").attr("name") == 'coverImage'){
					formData[ el.id ] = 'img/' + $( el ).val(); 
				} else if ($("#coverImage").attr("name") != 'coverImage') {
					formData[ el.id ] = $( el ).val();
				}
			}
		});

		this.collection.add( new app.Book( formData ) );
	},
  • Solution

Sorted :)

	addBook: function( e ) {
		e.preventDefault();

		var formData = {};

		$( '#addBook div' ).children( 'input' ).each( function( i, el ) {
			if( $( el ).val() != '' )
			{
				if( el.id === 'coverImage') {
					formData[ el.id ] = 'img/' + $( el ).val(); 
				} else {
					formData[ el.id ] = $( el ).val();
				}
			}
		});

		this.collection.add( new app.Book( formData ) );
	},

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.