Example #1
0
    solve: function() {
      if (this.model.get('solving')) {
        return true;
      }

      this.model.set('solving', true);

      var solution = new Solution({
        board: this.model.get('board')
      });

      solution.once('change', function() {
        var moves = solution.get('moves');
        this.model.get('board').move(moves.shift());

        var interval = setInterval(function() {
          this.model.get('board').move(moves.shift());

          if (moves.length === 0) {
            this.model.set('solving', false);
            clearInterval(interval);
          }
        }.bind(this), 300);
      }.bind(this));

      solution.fetch();
    },
Example #2
0
    it('generates the correct API request', function() {
      var solution = new Solution({ board: board });
      solution.fetch();

      var request = jasmine.Ajax.requests.mostRecent();
      expect(request.url).toBe('/api/v1/solutions');
      expect(request.method).toBe('POST');
      expect(request.params).toEqual(JSON.stringify({ board: [1, 0, 2, 3, 4, 5, 6, 7, 8] }));

      jasmine.Ajax.requests.mostRecent().respondWith({
        status: 200,
        contentType: 'application/json',
        responseText: '["LEFT"]'
      });

      expect(solution.get('moves')).toEqual(['LEFT']);
    });