Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Adventure Time with JavaScript and Single Page ...

Adventure Time with JavaScript and Single Page Applications

We’ve all seen the phrase ‘Single Page Application’ or ‘SPA’ everywhere and everyone talks about how they’re the best thing since sliced bread, but what is an SPA really? Why are they the cat’s pajamas? In this session Carl will take a look at a few of the popular SPA frameworks out there: AngularJS, BackBoneJS, and Kendo UI. He will also cover what a SPA does in general, what makes each one of these libraries unique, and how they can be utilized in your web development today.

Carl Bergenhem

November 07, 2013
Tweet

More Decks by Carl Bergenhem

Other Decks in Programming

Transcript

  1. Adventure Time with JavaScript & Single Page Apps - Web

    Unleashed 2013 - Carl Bergenhem @carlbergenhem
  2. Utilizes hash fragments h t t p : / /

    m y u r l . c o m / # / p r o d u c t s /
  3. Backbone.Model v a r m y M o d e

    l = n e w B a c k b o n e . M o d e l ( { } ) ;
  4. Extend Method v a r C h a r a

    c t e r = B a c k b o n e . M o d e l . e x t e n d ( { d e f a u l t s : { n a m e : ' B l a n k s ' , s p e c i e s : ' H u m a n ' , d e s c r i p t i o n : ' N o d e s c r i p t i o n y e t . ' } , i n i t i a l i z e : f u n c t i o n ( ) { c o n s o l e . l o g ( ' N e w c h a r a c t e r c r e a t e d ' ) ; } } ) ; v a r j a k e = n e w C h a r a c t e r ( { n a m e : ' J a k e t h e D o g ' , s p e c i e s : ' D o g ' , d e s c r i p t i o n : ' A m a g i c a l d o g . B e s t f r i e n d , a n d b r o t h e r , w i t h F i n n . ' } ) ;
  5. Use get() or set() to change data v a r

    f i n n = n e w C h a r a c t e r ( { n a m e : ' F i n n t h e H u m a n ' } ) ; f i n n . s e t ( { s p e c i e s : ' H u m a n ' } ) ; v a r n a m e = f i n n . g e t ( ' n a m e ' ) ; / / F i n n t h e H u m a n
  6. Backbone.Collection v a r C h a r a c

    t e r = B a c k b o n e . M o d e l . e x t e n d ( { d e f a u l t s : { n a m e : ' B l a n k s ' , s p e c i e s : ' H u m a n ' , d e s c r i p t i o n : ' N o d e s c r i p t i o n y e t . ' } , i n i t i a l i z e : f u n c t i o n ( ) { c o n s o l e . l o g ( ' N e w c h a r a c t e r c r e a t e d ' ) ; } } ) ; v a r C a s t = B a c k b o n e . C o l l e c t i o n . e x t e n d ( { m o d e l : C h a r a c t e r } ) ; v a r j a k e = n e w C h a r a c t e r ( { n a m e : ' J a k e t h e D o g ' , s p e c i e s : ' D o g ' , d e s c r i p t i o n : ' A m a g i c a l d o g . B e s t f r i e n d , a n d b r o t h e r , w i t h F i n n . ' } ) ; v a r f i n n = n e w C h a r a c t e r ( { n a m e : ' F i n n t h e H u m a n ' , s p e c i e s : ' H u m a n ' , d e s c r i p t i o n : ' T h e l a s t h u m a n i n t h e L a n d o f O o o . H a n g s o u t w i t h J a k e . ' } ) ; v a r c a s t L i s t = n e w C a s t ( [ j a k e , f i n n ] ) ; c o n s o l e . l o g ( c a s t L i s t . m o d e l s ) ;
  7. The 'el' property Every view has an 'el' property If

    not defined, a blank div is created References the view's DOM object Pass in a HTML element to modify it Can be a jQuery object via $el
  8. Backbone.View v a r F i n n V i

    e w = B a c k b o n e . V i e w . e x t e n d ( { t a g N a m e : ' d i v ' , i d : ' f i n n D i v ' , c l a s s N a m e : ' f i n n C l a s s ' , a t t r i b u t e s : { ' d a t a - c u s t o m ' : 1 2 3 4 5 } } ) ; v a r q u i c k V i e w = n e w F i n n V i e w ( ) ; $ ( ' # p l a c e h o l d e r ' ) . p r e p e n d ( q u i c k V i e w . e l ) ;
  9. Resulting HTML < d i v d a t a

    - c u s t o m = " 1 2 3 4 5 " i d = " s a m p l e D i v " c l a s s = " s a m p l e C l a s s " > < / d i v >
  10. Use of hash fragments h t t p : /

    / m y u r l . c o m / # / c h a r a c t e r s /
  11. Backbone.Router v a r S a m p l e

    R o u t e r = B a c k b o n e . R o u t e r . e x t e n d ( { r o u t e s : { ' c h a r a c t e r / : n a m e ' : ' c h a r a c t e r ' / / r o u t e p a t t e r n : h a n d l e r n a m e } , c h a r a c t e r : f u n c t i o n ( n a m e ) { / / h a n d l e r / / . . . } } ) ;
  12. One-way binding < d i v n g - c

    o n t r o l l e r = " S a m p l e C o n t r o l l e r " > < s p a n > { { c h a r a c t e r . n a m e } } < / s p a n > < / d i v >
  13. Two-way binding < s e l e c t n

    g - m o d e l = " s e l e c t e d C h a r a c t e r " > < o p t i o n v a l u e = " 0 " > F i n n < / o p t i o n > < o p t i o n v a l u e = " 1 " > J a k e < / o p t i o n > < o p t i o n v a l u e = " 2 " > B M O < / o p t i o n > < / s e l e c t >
  14. Sample Controller v a r s a m p l

    e A p p C o n t r o l l e r s = a n g u l a r . m o d u l e ( ' s a m p l e A p p C o n t r o l l e r s ' , [ ] ) ; s a m p l e A p p C o n t r o l l e r s . c o n t r o l l e r ( ' S h o w C o n t r o l l e r ' , [ ' $ s c o p e ' , f u n c t i o n S h o w C o n t r o l l e r ( $ s c o p e ) { $ s c o p e . s a m p l e V a r i a b l e = 1 ; $ s c o p e . s a m p l e A r r a y = [ { f i e l d O n e : 0 , f i e l d T w o : " T e s t " } , { f i e l d O n e : 1 , f i e l d T w o : " S a m p l e " } ] $ s c o p e . s a m p l e F u n c t i o n : f u n c t i o n ( ) { / / F u n c t i o n l o g i c } } ] ) ;
  15. Simple structure for routing v a r s a m

    p l e A p p = a n g u l a r . m o d u l e ( ' s a m p l e A p p ' , [ ' n g R o u t e ' , ' m y C o n t r o l l e r s ' ] ) ; ; s a m p l e A p p . c o n f i g ( [ ' $ r o u t e P r o v i d e r ' , f u n c t i o n ( $ r o u t e P r o v i d e r ) { $ r o u t e P r o v i d e r . w h e n ( ' / t e s t ' , { t e m p l a t e U r l : ' p a r t i a l s / t e s t . h t m l ' , c o n t r o $ r o u t e P r o v i d e r . w h e n ( ' / s a m p l e : i d ' , { t e m p l a t e U r l : ' p a r t i a l s / s a m p l e . h t m l ' , $ r o u t e P r o v i d e r . o t h e r w i s e ( { r e d i r e c t T o : ' / ' } ) ; } ] ) ;
  16. kendo.observable v a r f i n n M o

    d e l = k e n d o . o b s e r v a b l e ( { n a m e : ' F i n n t h e H u m a n ' , s p e c i e s : ' H u m a n ' , d e s c r i p t i o n : ' T h e l a s t h u m a n i n t h e L a n d o f O o o . H a n g s o u t w i t h J a k e . ' } ) ;
  17. Use get() or set() with fields v a r f

    i n n M o d e l = k e n d o . o b s e r v a b l e ( { n a m e : ' F i n n t h e H u m a n ' , s p e c i e s : ' D o g ' , d e s c r i p t i o n : ' T h e l a s t h u m a n i n t h e L a n d o f O o o . H a n g s o u t w i t h J a k e . ' } ) ; f i n n M o d e l . s e t ( ' s p e c i e s ' , ' H u m a n ' ) ; v a r s p e c i e s = f i n n M o d e l . g e t ( ' s p e c i e s ' ) ; / / H u m a n
  18. kendo.View() v a r i n d e x =

    n e w k e n d o . V i e w ( ' < s p a n > A d v e n t u r e T i m e ! < / s p a n > ' ) ; i n d e x . r e n d e r ( ' # p l a c e h o l d e r ' ) ;
  19. kendo.Layout v a r v i e w = n

    e w K e n d o . V i e w ( ' < s p a n > F i n n t h e H u m a n < / s p a n > ' ) ; v a r l a y o u t = n e w k e n d o . L a y o u t ( ' < h e a d e r > H e a d e r < / h e a d e r > < d i v i d = " c o n t e n t " > < / d i v > < f o o t e r > F o o t e r < / f o o t e r > ' ) ; l a y o u t . r e n d e r ( $ ( ' # a p p ' ) ) ; l a y o u t . s h o w I n ( ' # c o n t e n t ' , v i e w ) ;
  20. Use of hash fragments h t t p : /

    / m y u r l . c o m / # / c h a r a c t e r s /
  21. kendo.Router() v a r r o u t e r

    = n e w k e n d o . R o u t e r ( ) ; r o u t e r . r o u t e ( ' / ' , f u n c t i o n ( ) { l a y o u t . s h o w I n ( ' # c o n t e n t ' , h o m e V i e w ) ; } ) ; r o u t e r . r o u t e ( ' / c h a r a c t e r ' , f u n c t i o n ( ) { l a y o u t . s h o w I n ( ' # c o n t e n t ' , c h a r a c t e r V i e w ) ; } ) ; $ ( f u n c t i o n ( ) { r o u t e r . s t a r t ( ) ; / / v e r y i m p o r t a n t r o u t e r . n a v i g a t e ( ' / c h a r a c t e r ' ) ; } ) ;