ympbyc / LittleSmallscript

JavaScript with Smalltalk's syntax

Home Page:http://ympbyc.github.io/LittleSmallscript/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

is there a way to use littlesmallscript with angularjs?

zcaudate opened this issue · comments

basically,

i can't write the equivalent code in lss:

http://angularjs.org/

    function TodoCtrl($scope) {
    $scope.todos = [
    {text:'learn angular', done:true},
    {text:'build an angular app', done:false}];
     
    $scope.addTodo = function() {
    $scope.todos.push({text:$scope.todoText, done:false});
    $scope.todoText = '';
    };
     
    $scope.remaining = function() {
    var count = 0;
    angular.forEach($scope.todos, function(todo) {
    count += todo.done ? 0 : 1;
    });
    return count;
    };
     
    $scope.archive = function() {
    var oldTodos = $scope.todos;
    $scope.todos = [];
    angular.forEach(oldTodos, function(todo) {
    if (!todo.done) $scope.todos.push(todo);
    });
    };
    }

because blocks are not 'equivalent' to functions. is there a workaround?

Hi Chris. Thanks for your commitment.

I have not used angular yet so I might be missing the point, but I hope the following code helps.

| TodoCtrl |
TodoCtrl := [
  $scope at:#todos put: #(
    #{#text: 'learn angular', #done: true},
    #{#text: 'build an angular app', #done: false}
  ).

  $scope at:#addTodo put: [
    ($scope at:#todos) push: #{#text: $scope at:#todoText, #done: false}.
    $scope at:#todoText put: ''
  ]
].

$scope at:#remaining put:[
  | count |
  count := 0.
  angular for: ($scope at:#todos) Each: [:todo |
    count += ((todo at:#done) ifTrue: [0] ifFalse: [1])
  ].
  count
].

$scope at:#archive put:[
  | oldTodos |
  oldTodos := $scope at:#todos.
  $scope at:#todos put:#().
  angular for:oldTodos Each: [:todo |
    (todo at:#done) ifTrue: [
      ($scope at:#todos) push:todo
    ]
  ]
]

This should translate to the JS code equivalent to what you presented.
However I would recommend to use LSs when you want to write a 'Classy' code because LSs might not be the best tool for functional style coding.

Or if you are talking about the global variables, you could write

| $scope |
$scope := window at:#$scope

to ensure $scope is a property of the window object (i.e. global object).

Otherwise you could just remove the 'use strict' string at the start of the compiled JS to allow the use of global variables.

Thanks for your help Minori,

I had trouble with the block functions because js expects them a certain way.

lss is definitely 'classy' (in the elegant sense) and it makes writing javascript fun again!

will try your suggestions later today.

On 22/10/2012, at 12:56 AM, Minori Yamashita wrote:

Or if you are talking about the global variables, you could write

| $scope |
$scope := window at:#$scope
to make $scope a property of the window object (i.e. global object).


Reply to this email directly or view it on GitHub.