0

我遇到了一个问题,应该有一个简单的解决方案。出于某种原因,我的动作助手没有连接到它的方法。

这是我的 JSBin http://jsbin.com/UMaJaM/5/edit

代码复制如下以供参考。

HTML:

<!DOCTYPE html>
<html>
<head>
<meta name="description" content="Ember template" />
<meta charset=utf-8 />
<title>JS Bin</title>
  <script src="http://code.jquery.com/jquery-1.9.0.js"></script>
  <script src="http://builds.emberjs.com/handlebars-1.0.0.js"></script>
  <script src="http://builds.emberjs.com/tags/v1.1.2/ember.js"></script>
</head>
<body>
  <div id="main"></div>
</body>
</html>

JavaScript:

var TemplatedViewController = Ember.Object.extend({
    templateFunction: null,
    context: null,
    viewBaseClass: Ember.View,
    view: function () {
        var controller = this;
        var context = this.get('context') || {};
        var args = {
            template: controller.get('templateFunction'),
            controller: controller
        };
        args = $.extend(context, args);
        return this.get('viewBaseClass').extend(args);
    }.property('templateFunction'),
    appendView: function (selector) {
        this.get('view').create().appendTo(selector);
    },
    appendViewToBody: function (property) {
        this.get(property).create().append();
    }
});

var template_source = '<button type="button" class="btn" {{action "button"}}>Click</button>';

var MyController = TemplatedViewController.extend({
    templateFunction: Ember.Handlebars.compile(template_source),
    button: function() {
        console.log('hello world');
    }
});

var controller = MyController.create();

$(function () {
    controller.appendView('#main');
});
4

1 回答 1

1

You need to create an Ember application. Add this to the beginning of your script:

App = Ember.Application.create();
于 2013-11-01T04:18:30.993 回答