F2-javascript-textbook /class_8/bb_ng.md

Language MARKDOWN Lines 182
MD5 Hash 272de6e6f35a039b0f1404d92ee0bc1a
Repository https://github.com/codefellows/F2-javascript-textbook.git View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#Backbone and Angular

- [MVC Design Pattern](http://blog.codinghorror.com/understanding-model-view-controller/)
- [MOVE Design Pattern](http://cirw.in/blog/time-to-move-on)

## Tutorials
- [Angular Videos](https://egghead.io/)
- [Backbone Tutorial](http://adrianmejia.com/blog/2012/09/11/backbone-dot-js-for-absolute-beginners-getting-started)

##Code Snippets

### Angular

ng.html
```html
<!doctype html>
<html ng-app>
  <head>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.0-beta.7/angular.min.js"></script>
    <script src="ngTabs.js"></script>
    <link rel="stylesheet" href="ngTabs.css">
  </head>
  <body>
    <h2>Tabs</h2>
    <div ng-controller="NgTabsCtrl">
      <div ng-show="tabs.length === 0">
        Loading...
      </div>
      <div ng-show="tabs.length > 0">
        <div ng-repeat="tab in tabs">
          <div class="tab" ng-class="{active: tab.name === activeTab.name}">
            <a class="tab-link" href="#" ng-click="showTab(tab)">{{tab.name}}</a>
            <div class="hidden">{{tab.content}}</div>
          </div>
        </div>
        <div class="active-tab-content">
          {{activeTab.content}}
        </div>
      </div>
    </div>
  </body>
</html>
```

ng.js
```javascript
function NgTabsCtrl($scope, $http) {
  
  $scope.tabs = [];

  $http({method: 'GET', url: 'http://rs.hankyates.com:3000/content'})
    .success(onTabsFetched);

  $scope.showTab = function(tab){
    $scope.activeTab = tab;
  };

  function onTabsFetched(tabs){
    $scope.tabs = tabs;
    $scope.activeTab = $scope.tabs[0];
  }

};
```

### Backbone

```html
<!doctype html>
<html class="no-js">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title>Ajax Tabs Using Backbone</title>
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="stylesheet" href="app.css">
    </head>
    <body>
        <div id="tabs-app">
            <div class="hide-on-load">Loading...</div>
            <div class="show-on-load">
                <div class="tabs"></div>
                <div class="active-tab-content"></div>
            </div>
        </div>
        <script src="http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.6.0/underscore-min.js"></script>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
        <script src="http://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.2/backbone-min.js"></script>
        <script src="app.js"></script>
        <script type="text/template" id="tab-template">
            <div class="tab">
                <a href="#" class="tab-link"><%= name %></a>
                <div class="tab-content hidden"><%= content %></div>
            </div>
        </script>
    </body>
</html>
```

```javascript
$(function(){

	var TabModel = Backbone.Model.extend({
		defaults: {
			name: 'Unnamed Tab',
			content: 'Tab content not specified.'
		}
	});

	var TabsCollection = Backbone.Collection.extend({
		model: TabModel,
		url: 'http://rs.hankyates.com:3000/content'
	});

	var TabView = Backbone.View.extend({

		template: _.template($('#tab-template').html()),

		initialize: function(){
			this.render();
		},

		render: function(){
			this.$el.html(this.template(this.model.attributes));
	   		return this;
		}
	});

	var AppView = Backbone.View.extend({

		el: '#tabs-app',

		events: {
			'click .tab-link': 'tabClickHandler'
		},

		initialize: function(){
			var self = this;
			this.$tabsList = this.$('.tabs');
			this.$activeTabContent = this.$('.active-tab-content');
			this.tabs = new TabsCollection;
			this.listenTo(this.tabs, 'add', this.addTab);
			this.tabs.fetch({ success: function(){
				self.render();
			}});
		},

		render: function(){
			var $firstTab = $('.tab').first();
			this.activateTab($firstTab);
			this.$el.addClass('loaded');
		},
		
		addTab: function(tab){
			var tabView = new TabView({ model: tab });
			this.$tabsList.append(tabView.el);
		},

		tabClickHandler: function(e){
			e.preventDefault();
			this.activateTab($(e.target).closest('.tab'));
		},

		activateTab: function($tab){
			var tabContent = $tab.find('.tab-content').html(),
				$allTabs = this.$el.find('.tab');

			this.$activeTabContent.html(tabContent);
			
			$allTabs.removeClass('active');

			$tab.addClass('active');
		}


	});

	new AppView();

});
```
Back to Top