简体   繁体   English

为什么在浏览器中看不到我的App渲染(Marionette.js教程)?

[英]Why I can't see my App rendering in the browser(Marionette.js tutorial)?

I'm new in Marionette.js and I've been following the tutorial that is in the web page of marionette which is based in the 2.4 version and somethings are changed since this version. 我是Marionette.js的新手,我一直在遵循marionette网页上的教程,该教程基于2.4版本,并且自该版本以来有所更改。 The current version is 3 (the one I need to learn), so I've followed the todo tutorial and while doing it I tried to check out the documentation of the version 3 and changed some things to make the tutorial work for the new version. 当前版本是3(我需要学习的版本),因此我遵循了todo教程,在这样做的同时,我尝试查看版本3的文档并进行了一些更改以使该教程适用于新版本。

The Problem: 问题:

I cant see my app in the browser and also I get no console errors, I may not understand everything in the version 3 documentation because I am not to experienced but I need to learn this for my new job. 我在浏览器中看不到我的应用程序,也没有任何控制台错误,我可能不了解版本3文档中的所有内容,因为我没有经验,但是我需要为新工作学习这些内容。 I hope someone can give me an advice of how to solve this problem. 我希望有人能给我一些有关如何解决此问题的建议。

Here is the code: 这是代码:

driver.js driver.js

var Mn = require('backbone.marionette');
var TodoView = require('./views/layout');
var ToDoModel = require('./models/todo');

var initialData = [
      { assignee: 'Scott', text: 'Write something'} ,
      { assignee: 'Andrew', text: 'do more' }
];

var App = new Mn.Application({
   onStart: function(options) {
     var todo = new TodoView({
          collection: new Backbone.Collection(options.initialData),
          model: new ToDoModel()
     });
     todo.render();
     todo.triggerMethod('show');
   }
});

App.start({ initialData: initialData });

views/layout.js views / layout.js

var Bb = require('backbone');
var Mn = require('backbone.marionette');
var ToDoModel = require('../models/todo');

var FormView = require('./form');
var ListView = require('./list');

var Layout = Mn.View.extend({
  el: '#app-hook',
  template: require('../templates/layout.html'),

  regions: {
     form: '.form',
     list: '.list'
  },

  collectionEvents: {
     add: 'itemAdded'
  },

  onShow: function() {
     var formView = new FormView({ model: this.model });
     var lisView = new ListView({ collection: this.collection });

     this.showChildView('form', formView);
     this.showChildView('líst', listView);
  },

  onChildviewAddTodoItem: function(child) {
     this.model.set({
         assignee: child.ui.assignee.val(),
         text: child.ui.text.val()
     }, { validate: true });

     var items = this.model.pick('assignee', 'text');
     this.collection.add(items);
  },

  itemAdded: function() {
     this.model.set({
         assignee: '',
         text: ''
     });
  }
});

module.exports = Layout;

views/form.js views / form.js

var Mn = require('backbone.marionette');

var FormView = Mn.View.extend({
   tagName: 'form',
   template: require('../templates/form.html'),

   triggers: {
   submit: 'add:todo:item'
   },

   modelEvents: {
      change: 'render'
   },

   ui: {
     assignee: '#id_assignee',
     text: '#id_text'
   }
});
module.exports = FormView;

views/list.js views / list.js

var Mn = require('backbone.marionette')

var ToDo = Mn.View.extend({
    tagName: 'li',
    template: require('../templates/todoitem.html')
});

var TodoList = Mn.CollectionView.extend({
    tagName: 'ul',
    childView: ToDo,
});

module.exports = TodoList;

models/todo.js 型号/todo.js

var Bb = require('backbone');

var ToDo = Bb.Model.extend({
  dafaults: {
    assignee: '',
    text: ''
  },

  validate: function(attrs) {
    var errors = {};
    var hasError = false;
    if (!attrs.assignee) {
        errors.assignee = 'assignee must be set';
        hasError = true;
    }
    if (!attrs.text) {
        errors.text = 'text must be set';
        hasError = true;
    }

    if (hasError) {
        return errors;
    }
  }
});

module.exports = ToDo;

Mn v3 does not have an onShow for views.. while it is being triggered here I would recommend you avoid it: https://github.com/marionettejs/guides/issues/43 Mn v3没有onShow的视图..虽然它在这里被触发,但我建议您避免使用它: https : //github.com/marionettejs/guides/issues/43

But you also need to make sure the DOM has a $('#app-hook') when the app is run. 但是,您还需要确保在运行应用程序时DOM具有$('#app-hook')

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

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