最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Do Backbone.js views require jQuery or Zepto? (Or: why am I getting “Uncaught TypeError: undefined is not a functio

programmeradmin2浏览0评论

I’m just starting out with Backbone.js. I’ve subclassed Backbone.Model and Backbone.View:

var Message = Backbone.Model.extend();

var MessageView = Backbone.View.extend({
    tagName: 'div',
    className: 'message',
    template: _.template('{{ html }}'),

    render: function(){
        this.template({
            html: this.model.html
        });
        this.el.className.append(' ' + this.model.type);

        return this;
    }
});

I’ve then attempted to create an instance of each:

var message = new Message({html: html, type: type});
var messageView = new MessageView({model: message});

The last line line causes an error (in Chrome 12): Uncaught TypeError: undefined is not a function. It traces this error back to the function f.extend.make in Backbone.js.

The Backbone.js documentation on view.make says:

Convenience function for creating a DOM element of the given type (tagName), with optional attributes and HTML content. Used internally to create the initial view.el.

  1. Does it require jQuery or Zepto?
  2. Could I remove this dependency by overriding view.make in my call to Backbone.View.extend?

I’m just starting out with Backbone.js. I’ve subclassed Backbone.Model and Backbone.View:

var Message = Backbone.Model.extend();

var MessageView = Backbone.View.extend({
    tagName: 'div',
    className: 'message',
    template: _.template('{{ html }}'),

    render: function(){
        this.template({
            html: this.model.html
        });
        this.el.className.append(' ' + this.model.type);

        return this;
    }
});

I’ve then attempted to create an instance of each:

var message = new Message({html: html, type: type});
var messageView = new MessageView({model: message});

The last line line causes an error (in Chrome 12): Uncaught TypeError: undefined is not a function. It traces this error back to the function f.extend.make in Backbone.js.

The Backbone.js documentation on view.make says:

Convenience function for creating a DOM element of the given type (tagName), with optional attributes and HTML content. Used internally to create the initial view.el.

  1. Does it require jQuery or Zepto?
  2. Could I remove this dependency by overriding view.make in my call to Backbone.View.extend?
Share Improve this question edited Nov 15, 2011 at 14:45 Paul D. Waite asked Jul 17, 2011 at 13:04 Paul D. WaitePaul D. Waite 99k57 gold badges202 silver badges271 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

1) The documentation states that it requires

either jQuery ( > 1.4.2) or Zepto.

2) The View Component is tightly coupled to the jQuery/Zepto API. You could reimplement it, but if you use backbone.js extensively, you will reimplement the whole interface.

But maybe it works with your small use-case, but becuase of the tight coupling I would not guarantee that it works.

You also can use the Spine.js instead backbone.

It's also patible with JQuery and Zepto, but isn't needed to templating.

Spine.js don't need underscore too, but you can add as plugin if you need.

To know more about a good review here.

Spine.js uses the controller concept to bind data model with elements.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论