Skip to content

Commit 4333a29

Browse files
author
Em01
committed
Look at backbone
1 parent 504566a commit 4333a29

File tree

6 files changed

+111
-0
lines changed

6 files changed

+111
-0
lines changed

Basics/arrays.js

+10
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,13 @@ var puzzlers = [
8080
];
8181

8282

83+
//return the sum of an arguments array
84+
var add = function() {
85+
var total = 0;
86+
for(i = 0; i<arguments.length; i++) {
87+
total += + arguments[i];
88+
}
89+
return total;
90+
};
91+
92+

backbone/basics.js

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
//to get the data
2+
$.getJson('/todo', function(data) {});
3+
4+
//to create a model class
5+
var TodoItem = Backbone.Model.extend({});
6+
7+
//model instance
8+
var todoItem = new TodoItem(
9+
{ description: 'test', id: 1}
10+
);
11+
12+
//get
13+
todoItem.get('description');
14+
15+
//set
16+
todoItem.set({description: 'new'});
17+
18+
//save
19+
todoItem.save();
20+
21+
//to create a view class
22+
var TodoItem = Backbone.view.extend({});
23+
24+
//instantiate
25+
var todoView = new TodoView({model: todoItem});
26+
27+
//render
28+
var TodoView = Backbone.View.extend({
29+
render: function(){
30+
var html = '<h3>' + this.model.get('description') + '</h3>';
31+
$(this.el).html(html);
32+
}
33+
});
34+
35+
var AppointmentView = Backbone.View.extend({
36+
render: function(){
37+
$(this.el).html('<li>' + this.model.get('title') + '</li>');
38+
}
39+
});
40+
41+
42+
//ur to get JSON data from model
43+
todoItem.url = '/todo';
44+
45+
//populate model from server
46+
todoItem.fetch();
47+
todoItem.get('description');
48+
49+
50+
51+
//fetching dtata from the server
52+
53+
var TodoITem = Backbone.Model.extend({urlRoot: '/todos'});
54+
55+
//fetch todo wit id of 1
56+
var todoItem = new TodoItem({id: 1});
57+
todoItem.fetch();
58+
59+
//update the todo
60+
todoItem.set({description: 'Pick up'});
61+
todoItem.save();
62+
63+
64+
65+
66+
67+
//Creating and Deleting a new Todo
68+
var todoItem = new TodoItem();
69+
todoItem.set({description: 'Fill prescription' });
70+
todoItem.save(); //post /todos with JSON params
71+
72+
todoItem.get('id');
73+
todoItem.destroy(); //DELETE/todos/2
74+
todoItem.toJSON(); //Get JSON from model
75+
76+
77+
//Set Default values
78+
79+
var TodoItem = Backbone.Model.extend({
80+
defaults: {
81+
description: 'Empty todo...',
82+
status: 'incomplete'
83+
}
84+
});
85+
86+
var todoItem = new TodoItem();
87+
todoItem.get('description');
88+
todoItem.set('status');

backboneTodo/index.html

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<div>
2+
<h3>Test</h3>
3+
</div>

backboneTodo/todoModel.js

Whitespace-only changes.

backboneTodo/todoView.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
var TodoView = Backboen.View.extend({
2+
render: function(){
3+
var html = '<h3>' + this.model.get('description') + '</h3>';
4+
$(this.el).html(html);
5+
}
6+
});
7+
8+
var todoView = new TodoView({ model: todoItem });
9+
todoView.render();
10+
console.log(todoView.el);
File renamed without changes.

0 commit comments

Comments
 (0)