Created
June 3, 2012 15:58
-
-
Save abuisman/2863979 to your computer and use it in GitHub Desktop.
Some example of Backbone and AMD for SO question
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// file: models/Task.js | |
define([], function(){ | |
var Task = Backbone.Model.extend({ | |
//do stuff | |
}); | |
return Task; | |
}); | |
// file: collections/Tasks.js | |
define(['models/Task'], function(TaskModel){ | |
var Tasks = Backbone.Collection.extend({ | |
model: TaskModel | |
}); | |
return new Tasks(); | |
}); | |
// file: views/TaskList.js | |
define(['collections/Tasks'], function(Tasks){ | |
var TaskList = Backbone.View.extend({ | |
render: function(){ | |
_.each(Tasks.models, function(task, index){ | |
// do something with each task | |
}); | |
} | |
}); | |
return new TaskList(); | |
}); | |
// file: views/statistics.js | |
define(['collections/Tasks'], function(Tasks){ | |
var TaskStats = Backbone.View.extend({ | |
el: document.createElement('div'), | |
// Note that you'd have this function in your collection normally (demo) | |
getStats: function(){ | |
totals = { | |
all: Tasks.models.length | |
done: _.filter(Tasks, function(task){ return task.get('done'); }); | |
}; | |
return totals; | |
}, | |
render: function(){ | |
var stats = this.getStats(); | |
// do something in a view with the stats. | |
} | |
}); | |
return new TaskStats(); | |
}); | |
// file: views/App.js | |
define(['views/TaskList'], function(TaskListView){ | |
var App = Backbone.View.extend({ | |
el: '#app', | |
initialize: function(){ | |
_.bindAll(this, 'render'); | |
}, | |
render: function(){ | |
TaskListView.render(); | |
} | |
}); | |
return new App(); | |
}); | |
// called in index.html | |
Require(['views/App'], function(AppView){ | |
window.app = AppView; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment