css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <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
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console