属性对象#&LT的“$”;对象>不在_.extend._ensureElement函数 [英] Property '$' of object #<Object> is not a function at _.extend._ensureElement

查看:90
本文介绍了属性对象#&LT的“$”;对象>不在_.extend._ensureElement函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过创建一个视图试图输出骨干模型的一些dafault值,但该节点显示错误对象#产权$是不是在_.extend._ensureElement功能。我可以展示模型的默认值直接为我做下code,但不能使用视图。要显示一个例子,我已经注释掉// MyView的=新的firstView();所以你可以看到输出,但很快我删除我得到错误的意见。请让我知道什么是失踪,东西我做错了。已经尝试在$包裹code(函数(){}),但没有运气。

 前preSS =要求('前preSS');
     $ =要求('jQuery的');
     _ =要求('下划线');
     骨干=要求('骨干');
     应用=前preSS();     app.use(如press.static(__目录名称+''));
     app.use(如press.bodyParser());
     app.listen(process.env.PORT || 3000);     人= Backbone.Model.extend({
        默认值:{
            '名':'乔博客',
            工作:Web开发
        }
     });     第一人称=新的Person();
     的firstView = Backbone.View.extend({        初始化:功能(){
            this.render();
        },
        渲染:功能(){
            的console.log(firstPerson.get('工作'));
            返回此;        }
     })     // MyView的=新的firstView();
     的console.log(firstPerson.get('名'));


解决方案

骨干代理了jQuery库。它通常通过搜索全球 $ 变量这样做。既然你在节点环境中执行code,你会希望到手动:

  VAR骨干=要求(骨干),
    $ =要求('jQuery的');骨干$ = $。


作为一个方面说明,你真的应该为了使局部变量的函数范围使用 VAR 关键字变量前面。 (请参阅this问题

i am trying to output some dafault values of a backbone Model via creating a View but the node is showing the error "Property '$' of object # is not a function at _.extend._ensureElement". I can show default values from model directly as i am doing in the code below but not by using view. To show an example i've commented out the //myView = new firstView(); so you can see the output but soon i remove the comments i get the error. Please let me know what is missing and something i am doing wrong. Already tried wrapping the code within $(function(){}) but no luck.

     express = require('express');
     $ = require('jQuery');
     _ = require('underscore');
     Backbone = require('Backbone');
     app = express();

     app.use(express.static(__dirname + ''));
     app.use(express.bodyParser());
     app.listen(process.env.PORT || 3000);

     Person = Backbone.Model.extend({
        defaults: {
            'name': 'Joe Blog',
            'job': 'Web Developer'
        }
     });

     firstPerson = new Person();
     firstView = Backbone.View.extend({

        initialize: function() {
            this.render();
        },
        render: function() {
            console.log(firstPerson.get('job'));
            return this;

        }
     })

     //myView = new firstView();
     console.log(firstPerson.get('name'));

解决方案

Backbone proxies the jQuery library. It usually does so by searching for a global $ variable. Since you execute code in the node environment, you will want to do that manually:

var Backbone = require('backbone'),
    $ = require('jQuery');

Backbone.$ = $;


As a side note, you really should use the var keyword in front of variables in order to make variables local to the function scope. ( See this question )

这篇关于属性对象#&LT的“$”;对象>不在_.extend._ensureElement函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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