Skip to content

Commit

Permalink
Scope
Browse files Browse the repository at this point in the history
  • Loading branch information
stephband committed Aug 12, 2024
1 parent 795e1c4 commit 4b7ce1c
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 14 deletions.
27 changes: 23 additions & 4 deletions examples/todomvc/js/app.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,25 @@
(function (window) {
'use strict';

// Your starting point. Enjoy the ride!
let id = 0;

})(window);
export default class TodoList {
// Items is a data proxy of an array. Literal tracks mutations to this proxy
// via a signal graph.
items = Data.of([]);

constructor() {

}

createItem(text) {
this.items.push({
id: ++id,
completed: false,
text
});
}

destroyItem(id) {
const i = this.items.findIndex(matches({ id }));
if (i > -1) this.items.splice(i, 1);
}
}
6 changes: 5 additions & 1 deletion examples/todomvc/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@
How was the app created? Anything worth sharing about the process of creating the app? Any spec violations?

This implementation of TodoMVC was created using Literal's `<template is="literal-html">`
element. All the logic is contained in two templates in `index.html`, demonstrating how
element. The view/controller logic is contained in two templates in `index.html`, while
the model is exported by `js/app.js`.


demonstrating how
well suited the `literal-html` element is to quick prototyping: import the element, start
writing HTML with literal expressions. Bosh. No `npm` or any building required.

Expand Down
21 changes: 20 additions & 1 deletion modules/scope.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,23 @@
import * as scope1 from '../scope/scope-fns.js';
import * as scope2 from '../scope/scope-dom.js';

export default Object.assign({}, scope1, scope2);
const scope = {};

// Add Math functions to scope
const descriptors = Object.getOwnPropertyDescriptors(Math);
let name;
for (name in descriptors) scope[name] = Math[name];

export default Object.assign(scope, scope1, scope2, {
// Override round(n) with round(n, factor)
round: (value, n = 1) => Math.round(value / n) * n,
// Add the sane versions of isFinite() and isNaN() from Number
isFinite: Number.isFinite,
isInteger: Number.isInteger,
isNaN: Number.isNaN,
// Add Object functions
assign: Object.assign,
entries: Object.entries,
keys: Object.keys,
values: Object.values
});
8 changes: 0 additions & 8 deletions scope/scope-fns.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,6 @@ ${ data.array.map(include('#template-id')) }

// Built-ins added to scope with shorter names for template brevity

export const assign = Object.assign;
export const entries = Object.entries;
export const keys = Object.keys;
export const values = Object.values;
export const ceil = Math.ceil;
export const floor = Math.floor;
export const round = (value, n = 1) => Math.round(value / n) * n;


// This is the base set of scope functions. These functions are already used by
// literal so they come at no cost to have them in scope by default. Functions
Expand Down

0 comments on commit 4b7ce1c

Please sign in to comment.