Skip to content

DEMO: Action helper. No more data-user-id needed. #104

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions dist/helpers.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ let helpers = {
return truthTest(params, bodies, context, (left, right) => {
return left <= right;
});
},
action(context, params, bodies) {
let {selector, type, method} = params;
type = type || 'click';
let frag = bodies.main(context);
frag.querySelector(selector).addEventListener(type, context[method].bind(context));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me know if this would be useless, but maybe you could allow binding to a different context:

let boundContext = params.context || context;
frag.querySelector(selector).addEventListener(type, context[method].bind(boundContext));

return frag;
}
};

Expand Down
38 changes: 18 additions & 20 deletions test/sandbox/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,24 @@
</head>
<body>
<div id="input">
<textarea id="template" placeholder="Template goes here"><h3>What's on reddit:</h3>
<ul>
{#reddit.data.children}
<li>
{#data.thumbnail}
<img src="{.}">
{/data.thumbnail}
<p>{data.author}</p>
<a href="{data.url}">{data.title}</a>
</li>
{:pending}
<li><h4>Reddit is still loading ... &#9731;</h4></li>
{/reddit.data.children}
</ul>
</textarea>
<textarea id="context" placeholder="Context goes here">{
reddit: fetch('http://www.reddit.com/.json?limit=10').then(function(res) {
return res.json();
})
}</textarea>
<textarea id="template" placeholder="Template goes here">{#friends}
{@action type="click" selector="button" method="connect"}
<p>Connect with {name}!</p>
<button>Click to connect</button>
{/action}
{/friends}</textarea>
<textarea id="context" placeholder="Context goes here">(function() {
function Friend(name) {
this.name = name;
this.connect = function(evt) {
console.log('You are now connected to ' + this.name);
};
}

return {
friends: [new Friend('Steven'), new Friend('Prash')]
};
})();</textarea>
<button id="render">Render</button>
</div>
<div id="output">
Expand Down