Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationEventAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
18
Help us understand the problem. What is going on with this article?

More than 5 years have passed since last update.

@hokaccha

BrowserifyでBackbone.jsを使うときのjQueryの解決

Browserifyで次のように書いたら動かなかった。(bowerとdebowerifyも使ってる)

app.js
var Backbone = require('backbone');

var view = new Backbone.View(); //=> Uncaught TypeError: Property '$' of object #<Object> is not a function

これはBackbone.jsの現在のバージョン(1.1.0)での$の解決がrequireを使わずに次のようになっているから。

backbone.js
Backbone.$ = root.jQuery || root.Zepto || root.ender || root.$;

※ ちなみにUnderscore.jsはrequireで解決されてる。(source

なのでjQueryは自分で解決してあげないとだめ。

app.js
var $ = require('jquery');
var Backbone = require('backbone');
Backbone.$ = $;

var view = new Backbone.View();

せっかくBrowserifyとBowerでいい感じに依存関係解決できるのにつらい感じはある。

と思ってたけど、Backboneのmasterではこの問題が解決されてる(source)。

ので、

$ bower install backbone#master

すれば、自動的にjQueryを解決してくれる。

はよリリースしてほしい。

18
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
18
Help us understand the problem. What is going on with this article?