Pen Settings

HTML

CSS

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

JavaScript

Babel includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

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

              
            
!

CSS

              
                
              
            
!

JS

              
                //
////////////////////////////////////////////////////////////////////////////////
// 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

Console