javascript - How to improve my Backbone code -



javascript - How to improve my Backbone code -

i'm starting develop in backbone.js. , still not know how write code using best practices.

my app working have issues, example, comparator not work.

i help create him better.

view:

todos.views.tasksview = backbone.view.extend({ template: jst['todos/index'], tagname: 'div', id: '#todos', initialize: function () { this.collection = new todos.collections.taskscollection(); this.collection.fetch({ reset: true }); this.render(); this.listento(this.collection, 'reset', this.render); this.listento(this.collection, 'change', this.render); this.listento(this.collection, 'sort', this.render); }, render: function () { $(this.el).html(this.template({ tasks: tasks[0].task })); homecoming this; } });

collection:

todos.collections.taskscollection = backbone.collection.extend({ model: todos.models.tasksmodel url: "/api/tasks.json", comparator: function (sort) { homecoming -(new date(sort.get('eventtime'))).gettime(); }, parse: function (response) { homecoming response; } });

model:

todos.models.tasksmodel = backbone.model.extend({ parse: function (response, options) { homecoming response; } });

router:

todos.routers.tasksrouter = backbone.router.extend({ routes: { '': 'index' }, initialize: function () { var viewtasks = new todo.views.tasksview({ model: todos.models.tasksmodel, el: '.tasks-list' }); } });

app.js:

window.todos = { models: {}, collections: {}, views: {}, routers: {}, initialize: function () { new todos.routers.tasksrouter(); backbone.history.start({}) }, $(document).ready(function () { todos.initialize(); });

inside views seek not have model or collection function fetch or save or set.this not view responsibility.

example : instead of have this.collection.fetch() within collection, this.

this.collection = new todos.collections.taskscollection(); this.collection.getsomething(); this.render(); this.listento(this.collection, 'reset', this.render); this.listento(this.collection, 'change', this.render); this.listento(this.collection, 'sort', this.render);

inside collection, add together method perform "fetch()" request. don't understand why you're declaring parse function.. aren't doing anything.. utilize parse function when want alter in json before backbone bind model/collection.

i can't see you're calling comparator function.. maybe 'sort' parameter invalid value.

inside model you're using parse again.. can't see reason.

your router ok. code.. beginner said. it's good.

i recommend utilize lo-dash instead of underscore... have more options work arrays.

try utilize require.js load js dependencies.. help lot. www.requirejs.org

hope helps.

javascript backbone.js

Comments

Popular posts from this blog

ruby on rails - Devise Logout Error in RoR -

c# - Create a Notification Object (Email or Page) At Run Time -- Dependency Injection or Factory -

model view controller - MVC Rails Planning -