Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ 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

Auto Save

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

              
                <chat-app></chat-app>

<script type="module">
import { Component, route, DefineMap, DefineList, realtimeRestModel } from "//unpkg.com/can@5/core.mjs";

const Message = DefineMap.extend("Message",{
    id: "number",
    name: "string",
    body: "string",
    created_at: "date"
});

Message.List = DefineList.extend("MessageList",{
    "#": Message
});

Message.connection = realtimeRestModel({
    url: {
        resource: 'https://chat.donejs.com/api/messages',
        contentType: 'application/x-www-form-urlencoded'
    },
    Map: Message,
    List: Message.List
});

const socket = io('https://chat.donejs.com');

socket.on('messages created', function(message){
    Message.connection.createInstance(message);
});
socket.on('messages updated', function(message){
    Message.connection.updateInstance(message);
});
socket.on('messages removed', function(message){
    Message.connection.destroyInstance(message);
});

Component.extend({
    tag: "chat-messages",
    view: `
        <h1 class="page-header text-center">
             Chat Messages
        </h1>
        <h5><a href="{{ routeUrl(page='home') }}">Home</a></h5>

        {{# if(this.messagesPromise.isPending) }}
            <div class="list-group-item list-group-item-info">
                <h4 class="list-group-item-heading">Loading…</h4>
            </div>
        {{/ if }}
        {{# if(this.messagesPromise.isRejected) }}
            <div class="list-group-item list-group-item-danger">
                <h4 class="list-group3-item-heading">Error</h4>
                <p class="list-group-item-text">{{ this.messagesPromise.reason }}</p>
            </div>
        {{/ if }}
        {{# if(this.messagesPromise.isResolved) }}
            {{# for(message of this.messagesPromise.value) }}
                <div class="list-group-item">
                    <h4 class="list-group3--item-heading">{{ message.name }}</h4>
                    <p class="list-group-item-text">{{ message.body }}</p>
                </div>
            {{ else }}
                <div class="list-group-item">
                    <h4 class="list-group-item-heading">No messages</h4>
                </div>
            {{/ for }}
        {{/ if }}

        <form class="row" on:submit="this.send(scope.event)">
            <div class="col-sm-3">
                <input type="text" class="form-control" placeholder="Your name"
                    value:bind="this.name"/>
            </div>
            <div class="col-sm-6">
                <input type="text" class="form-control" placeholder="Your message"
                    value:bind="this.body"/>
            </div>
            <div class="col-sm-3">
                <input type="submit" class="btn btn-primary btn-block" value="Send"/>
            </div>
        </form>`,
    ViewModel: {
        // Properties
        messagesPromise: {
            default(){
                return Message.getList({});
            }
        },
        name: "string",
        body: "string",

        // Methods
        send(event) {
            event.preventDefault();

            new Message({
                name: this.name,
                body: this.body
            }).save().then(() => {
                this.body = "";
            });
        }
    }
});

Component.extend({
    tag: "chat-app",
    view: `
        <div class="container">
            <div class="row">
                <div class="col-sm-8 col-sm-offset-2">
                    {{# eq(this.routeData.page, 'home') }}
                        <h1 class="page-header text-center" on:click="this.addExcitement()">
                            {{ this.message }}
                        </h1>
                        <a href="{{ routeUrl(page='chat') }}"
                            class="btn btn-primary btn-block btn-lg">
                            Start chat
                        </a>
                    {{ else }}
                        <chat-messages/>
                    {{/ eq }}
                </div>
            </div>
        </div>`,
    ViewModel: {
        // Properties
        message: {
            type: "string",
            default: "Chat Home"
        },
        routeData: {
            default(){
                route.register("{page}",{page: "home"});
                route.start();
                return route.data;
            }
        },
        // Methods
        addExcitement(){
            this.message = this.message + "!";
        }
    }
});
</script>
              
            
!

CSS

              
                chat-home {
  display: block;
}
chat-home h1.page-header { margin-top: 0; }
              
            
!

JS

              
                
              
            
!
999px

Console