从ScrollContainer管道事件

时间:2014-08-17 22:15:11

标签: javascript javascript-events famo.us piping

我正在尝试从封装在视图中的ScrollContainer管道鼠标点击,但不知怎的,没有发生任何事情。我刚开始玩Famo.us所以也许我错过了一些明显的东西。我可以直接从ScrollContainer中包含的表面进行管道,但我不确定是否可以这样做。有什么想法吗?

链接到runnable jsFiddle demo:

http://jsfiddle.net/nqxz596f/

代码本身:

define('main', function (require, exports, module) {
    var Engine          = require('famous/core/Engine');
    var Surface         = require('famous/core/Surface');
    var Transform       = require('famous/core/Transform');
    var View            = require('famous/core/View');
    var Scrollview      = require('famous/views/ScrollContainer');

    var context = Engine.createContext();
    var mainview = new View({
        size: [500, 500]
    });
    var scrollview = new Scrollview({
        scrollview: {direction:0, size: [undefined, 300]}
    });
    var surfaces = [];

    scrollview.sequenceFrom(surfaces);

    for (var i = 0; i < 40; i++) {
        var surface = new Surface({
             content: "Surface: " + (i + 1),
             size: [undefined, 200],
             properties: {
                 backgroundColor: "hsl(" + (i * 360 / 40) + ", 100%, 50%)",
                 lineHeight: "200px",
                 textAlign: "center"
             }
        });

        surface.pipe(scrollview);
        surfaces.push(surface);
    }

    mainview._eventInput.on('click', function(){
        console.log('click');
    });

    scrollview.pipe(mainview);
    mainview.add(scrollview);

    context.add(mainview);
});

1 个答案:

答案 0 :(得分:1)

在撰写本文时,ScrollContainer没有它自己的EventHandler。它使用event handlers from the ScrollView that it containsscrollcontainer.scrollview)。 ScrollContainer有一个ContainerSurface,它从scrollcontainer.scrollview的事件中订阅,并且是DOM元素,用于隐藏scrollview的溢出。

The below code in a new jsFiddle显示了设置ContainerSurface的正确方法。您不需要mainview来管理您的活动。

您有几个选择:

  • 将surface事件传递给scrollview事件输出(如下所示)并在scrollview上侦听事件
  • 或将表面事件传递给容器surface.pipe(scrollcontainer.container);并侦听容器中的事件scrollcontainer.container.on('click', ...

更正代码:

define('main', function (require, exports, module) {
    var Engine = require('famous/core/Engine');
    var Surface = require('famous/core/Surface');
    var Transform = require('famous/core/Transform');
    var View = require('famous/core/View');
    var ScrollContainer = require('famous/views/ScrollContainer');
    var Scrollview = require('famous/views/Scrollview');
    var Utility = require('famous/utilities/Utility');

    var context = Engine.createContext();

    var scrollcontainer = new ScrollContainer({

        scrollview: {
            direction: Utility.Direction.Y
        }
    });
    scrollcontainer.container.setSize([500, 500]);

    var surfaces = [];

    for (var i = 0; i < 40; i++) {
        var surface = new Surface({
            content: "Surface: " + (i + 1),
            size: [undefined, 200],
            properties: {
                backgroundColor: "hsl(" + (i * 360 / 40) + ", 100%, 50%)",
                lineHeight: "200px",
                textAlign: "center"
            }
        });

        surface.pipe(scrollcontainer.scrollview._eventOutput);
        surfaces.push(surface);
    }

    scrollcontainer.scrollview.on('click', function (e) {
        console.log('click ' + e.target.innerText);
    });

    scrollcontainer.sequenceFrom(surfaces);

    context.add(scrollcontainer);
});

备选方案:

    for (var i = 0; i < 40; i++) {
        var surface = new Surface({
            content: "Surface: " + (i + 1),
            size: [undefined, 200],
            properties: {
                backgroundColor: "hsl(" + (i * 360 / 40) + ", 100%, 50%)",
                lineHeight: "200px",
                textAlign: "center"
            }
        });

        surface.pipe(scrollcontainer.container);
        surfaces.push(surface);
    }

    scrollcontainer.container.on('click', function (e) {
        console.log('click ' + e.target.innerText);
    });

<强> jsFiddle to show event pipe to a View

for (var i = 0; i < 40; i++) {
    var surface = new Surface({
        content: "Surface: " + (i + 1),
        size: [undefined, 200],
        properties: {
            backgroundColor: "hsl(" + (i * 360 / 40) + ", 100%, 50%)",
            lineHeight: "200px",
            textAlign: "center"
        }
    });

    surface.pipe(scrollcontainer.container);
    surfaces.push(surface);
}

var mainview = new View({size: [500, 500]});

mainview.on('click', function (e) {
    console.log('click ' + e.target.innerText);
});

scrollcontainer.sequenceFrom(surfaces);

scrollcontainer.container.pipe(mainview._eventOutput);

context.add(scrollcontainer).add(mainview);