Backbone.js的是不是当创建视图实例构造错误 [英] Backbone.js is not a constructor error when create instance of view

查看:115
本文介绍了Backbone.js的是不是当创建视图实例构造错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新用户Backbone.js的和测试我怎么能解决呢,最近几天我在测试我怎么能使用航线通过收集来改变视图的数据。

在目前的情况下,我被套牢的一个问题,当我试图在router.js创建ScheduleView实例的控制台登录此错误消息:

 类型错误:ScheduleView不是构造函数

下面的三个页面调度模块{看法,收集,模型} +的router.js的code

路由器

  //文件名:router.js
定义([
    jQuery的,
    下划线,
    '骨干',
    应用程序/视图/时间表,
    应用程序/收藏/时间表
]函数($,_,骨干,ScheduleView,ScheduleCollection){
    VAR AppRouter = Backbone.Router.extend({
        路线:{
            //定义某些URL路径
            时间表:scheduleRoute',
            //默认
            *行动:默认路由
        },
        scheduleRoute:功能(){
            //创建集合的一个新实例
            //你需要设置的URL集合中,以及打服务器
            VAR schedulecollection =新ScheduleCollection();
            //集合中传递的观点预计,这
            的console.log(typeof运算(ScheduleView));
            VAR scheduleview =新ScheduleView({
                集合:schedulecollection
            });
           //无需调用渲染在这里
           //作为渲染是迷上了收藏上的复位事件
        },
        默认路由:功能(动作){
            //我们没有匹配的路由,让显示主页
            //DashboardView.render();
        }
    });    VAR初始化函数=(){
        VAR app_router =新AppRouter;
        Backbone.history.start();
    };
    返回{
        初始化:初始化
    };
});

视图

  //文件名:查看/时间表
定义([
    jQuery的,
    下划线,
    '骨干',
    应用程序/收藏/日程安排,
    的文字!模板/ schedule.html
]函数($,_,骨干,ScheduleCollection,ScheduleTemplate){    VAR scheduleView = Backbone.View.extend({
        EL:$(应用程序),
        初始化:功能(){
            //听复位事件,这将调用render
            this.listenTo(this.collection,复位,this.render);
            //获取将填充集合的集合
            与响应//
            this.collection.fetch();
        },
        渲染:功能(){
            成功加载')//console.log('schedule图。
            的console.log(this.collection);
        }
    });
});

集合

  //文件名:收藏品/时间表
定义([
    jQuery的,
    下划线,
    '骨干',
    应用程序/模型/时间表
]函数($,_,骨干,ScheduleModel){
    VAR ScheduleCollection = Backbone.Collection.extend({
        型号:ScheduleModel,
        网址:HTTP:// SAM客户端:8888 / SAM的客户端/ I /日程安排,
        初始化:功能(){
            //console.log('schedule收藏成功加载');
        }
    });
    返回ScheduleCollection;
});

示范

  //文件名:型号/时间表
定义([
    下划线,
    '骨干',
    应用程序/配置'],功能(_,骨干,配置){
    VAR ScheduleModel = Backbone.Model.extend({
        // 如果你有任何
        // idAttribute:someId
        //你可以离开这个因为是如果你设置了idAttribute
        //这将直接apprnded到url
        urlRoot:H​​TTP:// SAM客户端:8888 / SAM的客户端/ I /日程安排,
        默认值:{
            反馈:'N / A'
        },
        初始化:功能(){
            的console.log('调度模型加载成功);
        }
    });
    返回ScheduleModel;});


解决方案

看起来像你的 ScheduleView 未定义 - 你没有从模块中导出任何东西。你似乎忘记了行

 收益scheduleView;

I am a new user to backbone.js and testing how could i work around with it, the last few days i was testing how could i use route to change view data via a collection.

In the current situation i was stuck with an issue that when i am trying to create an instance of ScheduleView in the router.js the console log this error message:

TypeError: ScheduleView is not a constructor

Below code of the three pages of schedule module {view, collection, model} + the router.js

The router

// Filename: router.js
define([
    'jquery',    
    'underscore',
    'backbone',
    'app/views/schedule',
    'app/collections/schedule'
], function($, _, Backbone, ScheduleView, ScheduleCollection) {
    var AppRouter = Backbone.Router.extend({
        routes: {
            // Define some URL routes
            'schedule': 'scheduleRoute',
            // Default
            '*actions': 'defaultRoute'
        },
        scheduleRoute: function() {
            // Create a new instance of the collection
            // You need to set the url in the collection as well to hit the server
            var schedulecollection = new ScheduleCollection();
            // Pass in the collection as the view expects it
            console.log(typeof(ScheduleView));
            var scheduleview = new ScheduleView({
                collection: schedulecollection
            });            
           // No need of calling render here
           // as the render is hooked up to the reset event on collection          
        },
        defaultRoute: function(actions) {            
            // We have no matching route, lets display the home page
            //DashboardView.render();
        }
    });

    var initialize = function() {                
        var app_router = new AppRouter;
        Backbone.history.start();
    };
    return {
        initialize: initialize
    };
});

The View

// Filename: views/schedule
define([
    'jquery',
    'underscore',
    'backbone',
    'app/collections/schedule',
    'text!templates/schedule.html'
], function ($, _, Backbone, ScheduleCollection, ScheduleTemplate) {

    var scheduleView = Backbone.View.extend({        
        el: $(".app"),
        initialize: function () {
            // Listen to the reset event which would call render
            this.listenTo(this.collection, 'reset', this.render);
            // Fetch the collection that will populate the collection
            // with the response 
            this.collection.fetch();
        },
        render: function () {
            //console.log('schedule view loaded successfully');  
            console.log(this.collection);
        }
    });        
});

The collection

// Filename: collections/schedule
define([
    'jquery',
    'underscore',
    'backbone',
    'app/models/schedule'
], function ($, _, Backbone, ScheduleModel) {
    var ScheduleCollection = Backbone.Collection.extend({
        model: ScheduleModel,
        url: "http://sam-client:8888/sam-client/i/schedule",
        initialize: function () {
            //console.log('schedule collections loaded successfully');
        }
    });
    return ScheduleCollection;
});

The Model

// Filename: models/schedule
define([
    'underscore',
    'backbone',
    'app/config'], function (_, Backbone, Config) {
    var ScheduleModel = Backbone.Model.extend({
        // If you have any
        //idAttribute : 'someId'
        // You can leave this as is if you set the idAttribute
        // which would be apprnded directly to the url
        urlRoot: "http://sam-client:8888/sam-client/i/schedule",
        defaults: {
            feedback: 'N/A'
        },
        initialize: function () {
            console.log('schedule model loaded successfully');
        }
    });
    return ScheduleModel;

});

解决方案

Looks like your ScheduleView is undefined - you didn't export anything from the module. You seem to have forgotten the line

return scheduleView;

这篇关于Backbone.js的是不是当创建视图实例构造错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆