简体   繁体   English

如何改善我的骨干网代码

[英]How to improve my Backbone code

I'm starting to develop in Backbone.js. 我开始在Backbone.js中进行开发。 And I still do not know how to write code using best practices. 而且我仍然不知道如何使用最佳实践来编写代码。

My app is working but I have some issues, for example, the comparator does not work. 我的应用程序正在运行,但是存在一些问题,例如,比较器无法正常工作。

I would really help to make 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
        }));

        return this;
    }

});

Collection: 采集:

Todos.Collections.TasksCollection = Backbone.Collection.extend({

  model: Todos.Models.TasksModel
  url: "/api/tasks.json",
  comparator: function (sort) {
      return -(new Date(sort.get('eventTime'))).getTime();
  },

  parse: function (response) {
      return response;
  }

});

Model: 模型:

Todos.Models.TasksModel = Backbone.Model.extend({
  parse: function (response, options) {
      return 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: App.js:

window.Todos = {
    Models: {},
    Collections: {},
    Views: {},
    Routers: {},
    initialize: function () {
       new Todos.Routers.TasksRouter();
       Backbone.history.start({})
    },


$(document).ready(function () {
    Todos.initialize();
});

Inside your views try to not have model or collection function like fetch or save or set.This is not a view responsibility. 在视图内部尝试不具有模型或集合功能,例如获取或保存或设置,这不是视图责任。

Example : Instead of have this.collection.fetch() inside your collection, do something like this. 示例:不要在您的集合中包含this.collection.fetch(),而是要做类似的事情。

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 your collection, add a method to perform "fetch()" request. 在您的集合中,添加一个方法来执行“ fetch()”请求。 I don't understand why you're declaring the parse function.. you aren't doing anything.. I like to use parse function when I want change something that is in your JSON before backbone bind it to your model/collection. 我不明白为什么要声明parse函数..您什么都没做..我想在主干将其绑定到模型/集合之前更改JSON中的某些内容时使用parse函数。

I can't see where you're calling the comparator function.. maybe the 'sort' parameter is with an invalid value. 我看不到您在哪里调用比较器函数..也许'sort'参数的值无效。

Inside your model you're using parse again.. I can't see a reason. 在模型内部,您将再次使用解析。。我看不出原因。

Your router is ok. 您的路由器没问题。 I like your code.. for a beginner as you said. 如您所说,我喜欢您的代码。 it's really good. 真的很棒。

I recommend you use lo-dash instead of underscore... It have more options to work with arrays. 我建议您使用lo-dash而不是下划线...它具有使用数组的更多选项。

Try to use require.js to load your js dependencies.. It will help you a lot. 尝试使用require.js加载您的js依赖项。这将对您有很大帮助。 www.requirejs.org www.requirejs.org

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM