Site:Frontend:Controllers
From Metrixstream
(Difference between revisions)
MetrixJustin (Talk | contribs) (Created page with "= Introduction = A Controller is a javascript class that will manage all the necessary javascript. This class is broken down by functionality and should only be instantiated whe...") |
MetrixJustin (Talk | contribs) |
||
Line 14: | Line 14: | ||
this.urls = []; | this.urls = []; | ||
} | } | ||
− | + | ||
siteController.prototype.registerUrls = function(urls) { | siteController.prototype.registerUrls = function(urls) { | ||
this.urls = urls; | this.urls = urls; | ||
}; | }; | ||
− | + | ||
siteController.prototype.registerProfileFunctions = function() { | siteController.prototype.registerProfileFunctions = function() { | ||
var myThis = this; | var myThis = this; | ||
− | + | ||
$('#getUserState').click(function(e) { | $('#getUserState').click(function(e) { | ||
e.preventDefault(); | e.preventDefault(); | ||
Line 41: | Line 41: | ||
The | The | ||
+ | |||
+ | <script type="text/javascript"> | ||
+ | var site = new siteController('<?cs var:MS.site.auth.username ?>', '<?cs var:MS.activeUser.username ?>', '<?cs var:MS.activeContent.id'); | ||
+ | $(document).ready(function() { | ||
+ | site.registerUrls({ | ||
+ | 'someUrl':'http://somesite.com' | ||
+ | }); | ||
+ | }); | ||
+ | </script> |
Revision as of 10:34, 20 June 2012
Introduction
A Controller is a javascript class that will manage all the necessary javascript. This class is broken down by functionality and should only be instantiated when necessary.
Basic Site Controller
Example Controller:
<script type="text/javascript"> function siteController(authUser, publicUser, publicContentId) { this.authUser = authUser; this.publicUser = publicUser; this.contentId = publicContentId; this.urls = []; } siteController.prototype.registerUrls = function(urls) { this.urls = urls; }; siteController.prototype.registerProfileFunctions = function() { var myThis = this; $('#getUserState').click(function(e) { e.preventDefault(); $.ajax({ type: 'get', url: myThis.urls['userStateUrl'], data: { 'user': myThis.publicUser }, success: function(data) { alert('This user is '+ data.state); } }); }); }; </script>
In the example controller, siteController is instantiated with the authenticated username, the public username (if avaliable), and the public contentId (if avaliable) which are stored so they can be accessed throughout the class.
The
<script type="text/javascript">
var site = new siteController('<?cs var:MS.site.auth.username ?>', '<?cs var:MS.activeUser.username ?>', '<?cs var:MS.activeContent.id'); $(document).ready(function() { site.registerUrls({ 'someUrl':'http://somesite.com' }); });
</script>