aLight - Add Some Control

by lega911

HTML

<link rel="stylesheet" href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.no-icons.min.css">
<script src="http://angularlight.org/bin/alight_0.49b.min.js"></script>
<div al-app="todoCtrl2" class="well">
    <h3>ToDo</h3>

    <form class="form-inline" al-submit="append()">
        <input al-value="title" class="input-medium" type="text"/>
        <input class="btn" type="submit" value="Append" />
    </form>

    <ul>
        <li al-repeat="item in list">
            <input type="checkbox" al-checked="item.done" />
            <span al-css="done:item.done">{{item.title}}</span>
            <a href al-click="remove(item)">remove</a>
        </li>
    </ul>

    <p>Total: {{total()}} / {{list.length}}</p>
</div>

CSS

.done {
    color: gray;
    text-decoration: line-through;
}

JavaScript

alight.controllers.todoCtrl2 = function(scope) {
    var todoCtrl = alight.getController('todoCtrl');
    todoCtrl(scope);
    oldTotal = scope.total;
    scope.total = function() {
        return (scope.list.length - oldTotal()) + "'";
    };
};

alight.controllers.todoCtrl = function($scope) {
    $scope.list = [
        { title: 'first', done: true },
        { title: 'second', done: false }];

    $scope.append = function() {
        $scope.list.push({ title: $scope.title, done: false });
        $scope.title = '';
    };
    $scope.remove = function(item) {
        var i = $scope.list.indexOf(item);
        $scope.list.splice(i, 1);
    };

    $scope.total = function() {
        var total = 0;
        $.each($scope.list, function(_, item) {
            if(!item.done) total += 1
        });
        return total;
    };
};