Created
February 16, 2012 16:02
-
-
Save rafamvc/1846006 to your computer and use it in GitHub Desktop.
How I render my page specific JS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// On the page specific file: | |
// "<controller_name>.js", and you import it after the init.js on your application.js | |
// Replace the <controller_name> with the actual name | |
RELASPHERE.<controller_name> = { | |
init: function() { | |
// Your shared code for the whole controller goes here | |
}, | |
index: function() { | |
// Your code for the index page goes here | |
}, | |
edit: function() { | |
// Your code for the edit page goes here | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Where you see RELASPHERE you should use MY_APP on the controller_name.js. Somehow github doesnt let me edit that file because of the name. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<body data-controller="<%= controller_name %>" data-action="<%= action_name %>" class='<%= controller_name %>'> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// On the page specific file: | |
// "<controller_name>.js", and you import it after the init.js on your application.js | |
// Replace the <controller_name> with the actual name | |
MY_APP.<controller_name> = { | |
init: function() { | |
// Your shared code for the whole controller goes here | |
}, | |
index: function() { | |
// Your code for the index page goes here | |
}, | |
edit: function() { | |
// Your code for the edit page goes here | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// On the page specific file: | |
// "<controller_name>.js", and you import it after the init.js on your application.js | |
// Replace the <controller_name> with the actual name | |
MY_APP.<controller_name> = { | |
init: function() { | |
// Your shared code for the whole controller goes here | |
}, | |
index: function() { | |
// Your code for the index page goes here | |
}, | |
edit: function() { | |
// Your code for the edit page goes here | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This global variable will hold all your page specific functions | |
// I know that global vars suck, but I think this is a good and safe exception | |
MY_APP = { | |
}; | |
UTIL = { | |
exec: function( controller, action ) { | |
var ns = MY_APP, | |
action = ( action === undefined ) ? "init" : action; | |
if ( controller !== "" && ns[controller] && typeof ns[controller][action] == "function" ) { | |
ns[controller][action](); | |
} | |
}, | |
init: function() { | |
var body = document.body, | |
controller = body.getAttribute( "data-controller" ), | |
action = body.getAttribute( "data-action" ); | |
UTIL.exec( controller ); | |
UTIL.exec( controller, action ); | |
} | |
}; | |
$( document ).ready( UTIL.init ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment