From 64638e802236def3c4642afb73338031817a541f Mon Sep 17 00:00:00 2001 From: ArnaudBienner Date: Thu, 10 Sep 2020 15:46:04 +0200 Subject: [PATCH] Add syntax highlighting to README examples --- README.md | 53 +++++++++++++++++++++++++++-------------------------- 1 file changed, 27 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index a521c7e3..4b6bb92c 100644 --- a/README.md +++ b/README.md @@ -16,42 +16,43 @@ Include it in your document AFTER jQuery. Like Sinatra, a Sammy application revolves around 'routes'. Routes in Sammy are a little different, though. Not only can you define 'get' and 'post' routes, but you can also bind routes to custom events triggered by your application. You set up a Sammy Application by passing a Function to the `$.sammy` (which is a shortcut for the Sammy.Application constructor). +```javascript +$.sammy(function() { - $.sammy(function() { - - this.get('#/', function() { - $('#main').text('Welcome!'); - }); - - }); + this.get('#/', function() { + $('#main').text('Welcome!'); + }); +}); +``` Inside the 'app' function() `this` is the Application. This is where you can configure the application and add routes. Above, we defined a `get()` route. When the browser is pointed to `#/` the function passed to that route will be run. Inside the route function, `this` is a Sammy.EventContext. EventContext has a bunch of special methods and properties including a params hash, the ability to redirect, render partials, and more. In its coolness, Sammy can handle multiple chained asynchronous callbacks on a route. - - this.get('#/', function(context,next) { - $('#main').text('Welcome!'); - $.get('/some/url',function(){ - // save the data somewhere - next(); - }); - }, function(context,next) { - $.get('/some/other/url',function(){ - // save this data too - next(); - }); - }); - +```javascript +this.get('#/', function(context,next) { + $('#main').text('Welcome!'); + $.get('/some/url',function(){ + // save the data somewhere + next(); + }); +}, function(context,next) { + $.get('/some/other/url',function(){ + // save this data too + next(); + }); +}); +``` Once you've defined an application the only thing left to do is run it. The best-practice behavior is to encapsulate `run()` in a document.ready block: +```javascript +var app = $.sammy(...) - var app = $.sammy(...) - - $(function() { - app.run(); - }); +$(function() { + app.run(); +}); +``` This will guarantee that the DOM is loaded before we try to apply functionality to it.