marko icon indicating copy to clipboard operation
marko copied to clipboard

I don't know how to use it after compilation.

Open imsheng opened this issue 8 years ago • 3 comments

demo.marko


static {
  var states = [
    {
      name: 'Colorado',
      value: 'CO'
    },
    {
      name: 'California',
      value: 'CA'
    }
  ];
}

class {
  onInput() {
    this.state = {
      stateIndex: null
    };
  }

  handleSelectChange(event, selectEl) {
    if (selectEl.selectedIndex === 0) {
      this.state.stateIndex = null;
    } else {
      this.state.stateIndex = selectEl.selectedIndex - 1;
    }
  }
}

style.css {
  .state-icon {
    width: 75px;
    height: 50px;
  }
}

<div>
  <h3>Choose a state</h3>
  <select onChange('handleSelectChange')>
    <option value='' selected=(state.stateIndex == null)>(choose a state)</option>
    <for(index, stateInfo in states)>
      <option value=stateInfo.value selected=(state.stateIndex === index)>
        ${stateInfo.name}
      </option>
    </for>
  </select>

  <div if(state.stateIndex != null)>
    You selected: ${states[state.stateIndex].value}
  </div>
</div>

marko compile demo.marko


// Compiled using [email protected] - DO NOT EDIT
"use strict";

var marko_template = module.exports = require("marko/src/html").t(__filename),
    marko_component = {
    onInput: function () {
        this.state = { stateIndex: null };
    },
    handleSelectChange: function (event, selectEl) {
        if (selectEl.selectedIndex === 0) {
            this.state.stateIndex = null;
        } else {
            this.state.stateIndex = selectEl.selectedIndex - 1;
        }
    }
},
    marko_componentType = "/undefined$0/demo.marko",
    components_helpers = require("marko/src/components/helpers"),
    marko_renderer = components_helpers.r,
    marko_defineComponent = components_helpers.c,
    marko_helpers = require("marko/src/runtime/html/helpers"),
    marko_attr = marko_helpers.a,
    marko_forEachProp = require("marko/src/runtime/helper-forEachProperty"),
    marko_escapeXml = marko_helpers.x;

var states = [
  {
    name: 'Colorado',
    value: 'CO'
  },
  {
    name: 'California',
    value: 'CA'
  }
];

function render(input, out, __component, component, state) {
  var data = input;

  out.w("<div><h3>Choose a state</h3><select" +
    marko_attr("data-marko", {
      onchange: __component.d("change", "handleSelectChange", false)
    }, false) +
    "><option value=\"\"" +
    marko_attr("selected", state.stateIndex == null) +
    ">(choose a state)</option>");

  var for__4 = 0;

  marko_forEachProp(states, function(index, stateInfo) {
    var keyscope__5 = "[" + ((for__4++) + "]");

    out.w("<option" +
      marko_attr("value", stateInfo.value) +
      marko_attr("selected", state.stateIndex === index) +
      ">" +
      marko_escapeXml(stateInfo.name) +
      "</option>");
  });

  out.w("</select>");

  if (state.stateIndex != null) {
    out.w("<div>You selected: " +
      marko_escapeXml(states[state.stateIndex].value) +
      "</div>");
  }

  out.w("</div>");
}

marko_template._ = marko_renderer(render, {
    ___type: marko_componentType
  }, marko_component);

marko_template.Component = marko_defineComponent(marko_component, marko_template._);

marko_template.meta = {
    deps: [
      {
          type: "css",
          code: ".state-icon {\r\n    width: 75px;\r\n    height: 50px;\r\n  }",
          virtualPath: "./demo.marko.css",
          path: "./demo.marko"
        }
    ],
    id: "/undefined$0/demo.marko",
    component: "./demo.marko"
  };

imsheng avatar Sep 29 '18 08:09 imsheng

@imsheng do you want to use this template on the server side or client side?

If you are going to be using it on the server side, you need to require the template in your backend logic and render it.

ravitadi avatar Oct 30 '18 21:10 ravitadi

@ravitadi client side.

imsheng avatar Oct 31 '18 08:10 imsheng

https://markojs.com/docs/server-side-rendering/#bootstrapping-non-lasso Instruction here for running the bundle in the browser.

If you aren't doing any server-side rendering, then just call the render method like https://markojs.com/docs/rendering/

You can use the DOM functions to then append, insert, etc to the DOM. https://markojs.com/docs/components/#codeappendtotargetelcode

jasonmacdonald avatar Nov 06 '18 15:11 jasonmacdonald