简体   繁体   English

秘银你好世界MVC示例不起作用

[英]mithril hello world MVC example not working

I am having trouble getting the Mithril hello world MVC example working. 我在使Mithril hello world MVC示例工作时遇到困难。

Here is my code, as copied from the Mitrhil homepage . 这是从Mitrhil主页复制的代码。 Note the only change I made was to swap the m.request({method: "GET", url: "pages.json"}); 请注意,我所做的唯一更改是交换了m.request({method: "GET", url: "pages.json"}); method call to a manually generated pages object. 对手动生成的页面对象的方法调用。

//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);

As you can see, my example above in jsFiddle doesn't work, but another Mitrhil example, the todo app jsFiddle works fine. 如您所见,上面我在jsFiddle中的示例不起作用,但是在另一个Mitrhil示例中,todo应用程序jsFiddle可以正常工作。

I think it would make sense for the basic MVC Mitrhil example to work simply like the Todo app does, and possibly link to a jsFiddle or CodePen example for users to fork, similar to in React. 我认为,基本的MVC Mitrhil示例可以像Todo应用一样工作,并且可以链接到jsFiddle或CodePen示例供用户进行分叉,这与React中的示例类似。

There were some calls to pages which should have been variable references instead, since it's an Array. 有一些对pages调用应该是变量引用,因为它是一个数组。 Here's the fix: http://jsfiddle.net/jug68s27/4/ 解决方法如下: http : //jsfiddle.net/jug68s27/4/

ctrl.pages() -> ctrl.pages ctrl.pages() -> ctrl.pages

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

In this example the value not redraw because you didn't use m.prop , if you want that the value change you cas use var pages = m.prop(''); 在此示例中,该值不会重绘,因为您没有使用m.prop ,如果希望更改值,则可以使用var pages = m.prop(''); Now you can use pages().push or ctrl.pages().map because m.prop is a function!!!! 现在您可以使用pages().pushctrl.pages().map因为m.prop是一个函数! Remember this it's very important, you will use it a lot 请记住,这非常重要,您会经常使用它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM