秘银你好世界 MVC 示例不工作

mithril hello world MVC example not working

我在运行 Mithril hello world MVC 示例时遇到问题。

这是我的代码,从 Mitrhil homepage 复制而来。请注意,我所做的唯一更改是将 m.request({method: "GET", url: "pages.json"}); 方法调用交换为手动生成的页面对象。

//namespace
var app = {};

//model
app.PageList = function() {
    var pages = [];
    pages.push({title: 'page 1', url: '/page1.html'});
    pages.push({title: 'page 2', url: '/page2.html'});
    return pages;
};

//controller
app.controller = function() {
    var pages = app.PageList();
    return {
        pages: pages,
        rotate: function() {
            pages().push(pages().shift());
        }
    }
};

//view
app.view = function(ctrl) {
    return [
        ctrl.pages().map(function(page) {
            return m("a", {href: page.url}, page.title);
        }),
        m("button", {onclick: ctrl.rotate}, "Rotate links")
    ];
};

//initialize
m.module(document.getElementById("example"), app);

如您所见,我上面 jsFiddle doesn't work, but another Mitrhil example, the todo app jsFiddle 中的示例工作正常。

我认为基本的 MVC Mitrhil 示例可以像 Todo 应用程序一样简单地工作,并且可能 link 到 jsFiddle 或 CodePen 示例供用户分叉,类似于 React。

有一些对 pages 的调用应该是变量引用,因为它是一个数组。这是修复:http://jsfiddle.net/jug68s27/4/

ctrl.pages() -> ctrl.pages

pages().push(pages().shift()) -> pages.push(pages.shift())

在这个例子中,值没有重绘,因为你没有使用 m.prop,如果你想改变值,你可以使用 var pages = m.prop(''); 现在你可以使用pages().pushctrl.pages().map因为m.prop是一个函数!!!! 记住这个很重要,你会经常用到它