cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - Activehtmlicon-personicon-teamoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              <div id="movies">
  <ul>
    <li> <span class="fa fa-trash"></span> Groundhog day
    <li> Breakfast Club
  </ul>
</div>
<div id="mocha"></div>

            
          
!
            
              //
////////////////////////////////////////////////////////////////////////////////
// Model 
//
var model = {
  items: [{
    id: 1,
    name: "Terminator",
    description: "I'll be back"
  }, {
    id: 2,
    name: "Avatar",
    description: "They're just pissing on us without even giving us the courtesy of calling it rain"
  }],
  itemId: 2
};

model.present = function(data) {
  data = data || {};

  if (data.deletedItemId !== undefined) {
    var d = -1 ;
    model.items.forEach(function(el,index) {
      if (el.id !== undefined) {
        if (el.id == data.deletedItemId) {
          d = index ;       
        }
      }
    });
    if (d>=0) {
      model.lastDeleted = model.items.splice(d,1)[0] ;
    }
  }  
  if (data.item !== undefined) {
    if (data.item.id !== null) {
      // has been edited
      model.items.forEach(function(el,index) {
        if (el.id !== undefined) {
          if (el.id == data.item.id) {
            model.items[index] = data.item;
          }
        }
      });

    } else {
      // new item
      data.item.id = model.itemId++ ;
      model.items.push(data.item) ;
    }
  }
  
  state.render(model);
}

//
////////////////////////////////////////////////////////////////////////////////
// View
//
var view = {};

// Initial State
view.init = function(model) {
  return view.ready(model);
};

// State representation of the ready state
view.ready = function(model) {
  model.lastEdited = model.lastEdited || {};
  var nameValue = model.lastEdited.name || 'Name';
  var descriptionValue = model.lastEdited.description || 'Description';
  var id = model.lastEdited.id || '';
  var output = (
    '<ul>\n\
               ' + model.items.map(function(e) {
      return ('<li><a href="#" onclick="JavaScript:return actions.delete({\'id\':\''+e.id+'\'});"><i class="fa fa-trash"></i></a>  ' + e.name + '</li>');
    }).join('\n') + '\n\
             </ul>\n'
  );
  return output;
};

//display the state representation
view.display = function(representation) {
  var stateRepresentation = document.getElementById("movies");
  stateRepresentation.innerHTML = representation;
};

// Display initial state
view.display(view.init(model));

////////////////////////////////////////////////////////////////////////////////
// State
//
var state = {
  view: view
};

model.state = state;

// Derive the state representation as a function of the systen
// control state
state.representation = function(model) {
  var representation = 'oops... something went wrong, the system is in an invalid state';

  if (state.ready(model)) {
    representation = state.view.ready(model);
  }

  state.view.display(representation);
};

// Derive the current state of the system
state.ready = function(model) {
  return true;
};

state.render = function(model) {
  state.representation(model)
};

////////////////////////////////////////////////////////////////////////////////
// Actions
//

var actions = {};

actions.save = function(data, present) {
  present = present || model.present;
  data.item = { name: data.name, description: data.description, id: data.id || null };
  present(data);
  return false;
};
actions.delete = function(data, present) {
  present = present || model.present;
  data = {
    deletedItemId: data.id
  };
  present(data);
  return false;
};




////////////////////////////////////////////////////////////////////////////////
/// TESTS
//

mocha.setup('bdd');
var expect = chai.expect;

describe("Canary", function() {
  it('works', () => {
    expect(true).to.equal(true);
  });
});
describe("List", function() {
  it('Delete Single Item', deleteSingleItem);
  it('Add Single Item', addSingleItem);
  it('Edit Single Item', editSingleItem);

  function deleteSingleItem() {
    model.items = [];
    model.items.push({
        id: 1,
        name: "Foo",
        description: "Bar"
      });

    actions.delete({ id: 1 });

    expect(model.items.length).to.equal(0);
  };  
  function addSingleItem() {
    model.items = [];

    actions.save({ 
      name: 'Foo',
      description: 'Bar',
    });

    expect(model.items.length).to.equal(1);
    expect(model.items[0].name).to.equal('Foo');
  };  
  function editSingleItem() {
    model.items = [{
      id: 1,
      name: 'Foo',
      description: 'Bar'
    }];

    actions.save({ 
      id: 1,
      name: 'Foo2',
      description: 'Bar'
    });

    expect(model.items.length).to.equal(1);
    expect(model.items[0].name).to.equal('Foo2');
  };  
});
  
mocha.run();

            
          
!
999px
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console