Build up some UI with mithril.js and jsx

Multi tool use
Multi tool use












1















I'm new to javascript and its ecosystem. I'm trying to build some components using mithril.js. My goal is to have a component that shows some properties and provides a couple of button for each of them. Just to learn about mithril.js and jsx. Here is what I did so far:



const m = require("mithril");
var Something = {
_increase: function(category) {
console.log("increase category: "+category);
},
_decrease: function(category) {
console.log("decrease category: "+category);
},
view: function(vnode) {
return <div>
{Object.keys(vnode.attrs.categories).map((category)=> {
return <div>
<label for={category}>{category}</label>
<input type="number" id={category} value={vnode.attrs.categories[category]} />
<button type="button" onclick="{this._increase(category)}">MORE</button>
<button type="button" onclick="{this._decrease(category)}">LESS</button>
</div>
})}
</div>
}
}
export default Something;


Well, component seems to work fine, node doesn't complain and labels and buttons and fields are displayed on page, but, when I click on a button, nothing happen. It looks like event isn't fired. What's wrong?










share|improve this question



























    1















    I'm new to javascript and its ecosystem. I'm trying to build some components using mithril.js. My goal is to have a component that shows some properties and provides a couple of button for each of them. Just to learn about mithril.js and jsx. Here is what I did so far:



    const m = require("mithril");
    var Something = {
    _increase: function(category) {
    console.log("increase category: "+category);
    },
    _decrease: function(category) {
    console.log("decrease category: "+category);
    },
    view: function(vnode) {
    return <div>
    {Object.keys(vnode.attrs.categories).map((category)=> {
    return <div>
    <label for={category}>{category}</label>
    <input type="number" id={category} value={vnode.attrs.categories[category]} />
    <button type="button" onclick="{this._increase(category)}">MORE</button>
    <button type="button" onclick="{this._decrease(category)}">LESS</button>
    </div>
    })}
    </div>
    }
    }
    export default Something;


    Well, component seems to work fine, node doesn't complain and labels and buttons and fields are displayed on page, but, when I click on a button, nothing happen. It looks like event isn't fired. What's wrong?










    share|improve this question

























      1












      1








      1








      I'm new to javascript and its ecosystem. I'm trying to build some components using mithril.js. My goal is to have a component that shows some properties and provides a couple of button for each of them. Just to learn about mithril.js and jsx. Here is what I did so far:



      const m = require("mithril");
      var Something = {
      _increase: function(category) {
      console.log("increase category: "+category);
      },
      _decrease: function(category) {
      console.log("decrease category: "+category);
      },
      view: function(vnode) {
      return <div>
      {Object.keys(vnode.attrs.categories).map((category)=> {
      return <div>
      <label for={category}>{category}</label>
      <input type="number" id={category} value={vnode.attrs.categories[category]} />
      <button type="button" onclick="{this._increase(category)}">MORE</button>
      <button type="button" onclick="{this._decrease(category)}">LESS</button>
      </div>
      })}
      </div>
      }
      }
      export default Something;


      Well, component seems to work fine, node doesn't complain and labels and buttons and fields are displayed on page, but, when I click on a button, nothing happen. It looks like event isn't fired. What's wrong?










      share|improve this question














      I'm new to javascript and its ecosystem. I'm trying to build some components using mithril.js. My goal is to have a component that shows some properties and provides a couple of button for each of them. Just to learn about mithril.js and jsx. Here is what I did so far:



      const m = require("mithril");
      var Something = {
      _increase: function(category) {
      console.log("increase category: "+category);
      },
      _decrease: function(category) {
      console.log("decrease category: "+category);
      },
      view: function(vnode) {
      return <div>
      {Object.keys(vnode.attrs.categories).map((category)=> {
      return <div>
      <label for={category}>{category}</label>
      <input type="number" id={category} value={vnode.attrs.categories[category]} />
      <button type="button" onclick="{this._increase(category)}">MORE</button>
      <button type="button" onclick="{this._decrease(category)}">LESS</button>
      </div>
      })}
      </div>
      }
      }
      export default Something;


      Well, component seems to work fine, node doesn't complain and labels and buttons and fields are displayed on page, but, when I click on a button, nothing happen. It looks like event isn't fired. What's wrong?







      javascript jsx mithril.js






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Dec 29 '18 at 22:00









      FrancescoFrancesco

      66322654




      66322654
























          1 Answer
          1






          active

          oldest

          votes


















          0














          Two things: (1) I think you should just put the function into the onclick handler braces instead of encoding the function in a string. (2) It looks like you're immediately invoking the function, not declaring that the onclick handler is a function that uses the category argument. Try passing in an anonymous function with no arguments, that way you when the onclick event is fired it can take in the category as a parameter:



          onclick={() => this._increase(category)}
          onclick={() => this._decrease(category)}





          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53973666%2fbuild-up-some-ui-with-mithril-js-and-jsx%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            Two things: (1) I think you should just put the function into the onclick handler braces instead of encoding the function in a string. (2) It looks like you're immediately invoking the function, not declaring that the onclick handler is a function that uses the category argument. Try passing in an anonymous function with no arguments, that way you when the onclick event is fired it can take in the category as a parameter:



            onclick={() => this._increase(category)}
            onclick={() => this._decrease(category)}





            share|improve this answer




























              0














              Two things: (1) I think you should just put the function into the onclick handler braces instead of encoding the function in a string. (2) It looks like you're immediately invoking the function, not declaring that the onclick handler is a function that uses the category argument. Try passing in an anonymous function with no arguments, that way you when the onclick event is fired it can take in the category as a parameter:



              onclick={() => this._increase(category)}
              onclick={() => this._decrease(category)}





              share|improve this answer


























                0












                0








                0







                Two things: (1) I think you should just put the function into the onclick handler braces instead of encoding the function in a string. (2) It looks like you're immediately invoking the function, not declaring that the onclick handler is a function that uses the category argument. Try passing in an anonymous function with no arguments, that way you when the onclick event is fired it can take in the category as a parameter:



                onclick={() => this._increase(category)}
                onclick={() => this._decrease(category)}





                share|improve this answer













                Two things: (1) I think you should just put the function into the onclick handler braces instead of encoding the function in a string. (2) It looks like you're immediately invoking the function, not declaring that the onclick handler is a function that uses the category argument. Try passing in an anonymous function with no arguments, that way you when the onclick event is fired it can take in the category as a parameter:



                onclick={() => this._increase(category)}
                onclick={() => this._decrease(category)}






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Dec 29 '18 at 22:18









                JulianJulian

                135119




                135119






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53973666%2fbuild-up-some-ui-with-mithril-js-and-jsx%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    5xe,wmO k t67Cf XbSiVr2WSv,5xDP7Q06bkF,1 38Q 1xl,D5BcLQQjqzd3G1GUY5BgG,xjGU YjkE
                    njYqsNmWxsQt9R13Y84Ypa tMw EmaKM gX7lr26dnWVK

                    Popular posts from this blog

                    Monofisismo

                    Angular Downloading a file using contenturl with Basic Authentication

                    Olmecas