3.0.13 • Published 5 years ago

gap-front-view v3.0.13

Weekly downloads
3
License
MIT
Repository
github
Last release
5 years ago

gap-front-view

Install

yarn add gap-front-view

Quik Start

Bind HtmlElement's propertes and attributes

import {View} from 'gap-front-view';

class UserView extends Veiw {
    template() {
        return this.html`
            <form action="javascript:;">
                <input
                    name="userId"
                    bind-value="userId"
                    bind-id="userId"
                >
                <input
                    name="name"
                    bind-value="name"
                >
            </form>
        `;
    }
}

const userView = new UserView();

userView.update({
    userId: 'userId',
    name: 'Mike'
})

Bind text

import {View} from 'gap-front-view';

class UserView extends View {
    template() {
        return this.html`
        <div>
            $${'user.name'} - $${'user.address'}
        </div>
        `;
    }
}

const userView = new UserView();
userView.update({
    user: {
        name: 'tom',
        address: 'china'
    }
})

Include sub template

import {View} from 'gap-front-view';

class BookView extends View {
    template() {
        return this.html`
        <div>
            <span class="book-title">$${'book.title'}</span>
            <div class="book-author">
            ${this.getAuthorTpl()}
            </div>
        </div>
        `;
    }

    getAuthorTpl() {
        return this.html`
        <span>$${'book.author.name'} - $${'book.author.address'}</span>
        `;
    }
}

const bookView = new BookView();
bookView.update({
    book: {
        title: 'Real Analysis, Fourth Edition',
        author: {
            name: 'mike',
            address: 'usa'
        }
    }
});

Include sub view

Bind one property of current data

class UserView extends View {
    template() {
        return this.html`
        <div class="user-view">$${'name'} - $${'address'}</div>
        `;
    }
}

class BookView extends View {
    template() {
        return this.html`
        <div>
            <span class="book-title">$${'book.title'}</span>
            <div class="book-author">
            <gap-view
                view=${new UserView()}
                bind="book.author"
            ></gap-view>
            <div class="author">
                $${'book.author.name'} - $${'book.author.address'}
            </div>
            </div>
        </div>
        `;
    }
}

const bookView = new BookView();
bookView.appendTo(document.body);

bookView.update({
    book: {
        title: 'time history',
        author: {
            name: 'jack',
            address: 'yk'
        }
    }
});

Bind multi properties of current data

class CoverView extends View {
    template() {
        return this.html`
        <div class="cover">
            $${'title'} - $${'author.name'} - $${'author.address'}
        </div>
        `;
    }
}

class BookView extends View {
    template() {
        return this.html`
        <div>
            <span class="book-title">$${'book.title'}</span>
            <div class="book-author">
            <gap-view
                ref=${view => this.coverView = view}
                view=${new CoverView()}
                bind-multi=${{
                    title: 'book.title',
                    'author.name': 'book.author.name',
                    'author.address': 'book.author.address'
                }}
            ></gap-view>
            </div>
        </div>
        `;
    }
}

use component

import {View} from '../index';

class AuthorView extends View {
    template() {
        return this.html`
            <div class="author"><span>${this.props.name}</span>$${'age'}</div>
        `;
    }
}

class DetailView extends View {
    template() {
        return this.html`
            <div class="detail">$${'title'}</div>
        `;
    }
}

// global register
View.regComponent('author-view', AuthorView);

class BookView extends View {
    template() {
        return this.html`
            <div class="book">
                <author-view props=${{'name': 'gap'}} bind-age="age"></author-view>
                <book-detail bind-title="title"></book-detail>
            </div>
        `;
    }

    // local register
    component() { return {'book-detail': DetailView}; }
}

Handle array

class UserListView extends View {
    template() {
        return this.html`
        <div
            arr="users"
            item-as="user"
            item-key=${user => user.name}
        >
            ${() => this.html`
                <span bind-id="user.userId">
                    $${'user.name'}
                    -
                    $${'user.age'}
                    -
                    $${'user.address'}
                </span>
            `}
        </div>
        `;
    }
}

const userListView = new UserListView();
userListView.update({
    users: [
        {userId: 'id1', name: 'jack', age: 10, address: 'sh'},
        {userId: 'id2', name: 'rose', age: 21, address: 'sh'},
        {userId: 'id3', name: 'mike', age: 20, address: 'zj'}
    ]
});

userListView.data.users.push({userId: 'id4', name: '', ...});
userListView.data.users.pop();

// todo
// userListView.data.users.unshift({userId: 'id4', name: '', ...});

userListView.data.users.shift();
userListView.data.users.delete({userId: 'id3', name: '' ...});
userListView.data.users.filter(user => user.age > 18);

Use props to transfor data

class UserView extends View {
    construct(props) {
        super(props);
        // ...
        // coding here
    }

    template() {
        return this.html`
        <div class="${this.props.class}">
        $${'user.name'} - $${'user.address'}
        </div>
        `;
    }
}

const userView = new UserView({class: 'primary'});
userView.update({
    user: {
        name: 'rose',
        address: 'usa'
    }
})

todo

[] Implement feature like 'computed' -> jason

x Separate GapCommitTxn from norm Txn.

[] Replace 'gap-view' with 'custom-tag-name' -> sc

[] Prevent infinite loop of changing data caused by watcher -> zk

x GapArr.unshift(item)

x Alternate console.log for debug

[] Separate proxy/data from view -> jason

[] vdom / ast -> sc

3.0.13

5 years ago

3.0.12

5 years ago

3.0.11

5 years ago

3.0.10

6 years ago

3.0.9

6 years ago

3.0.8

6 years ago

3.0.7

6 years ago

3.0.6

6 years ago

3.0.5

6 years ago

3.0.4

6 years ago

3.0.3

6 years ago

3.0.2

6 years ago

3.0.1

6 years ago

3.0.0

6 years ago

2.8.1

6 years ago

2.8.0

6 years ago

2.7.6

6 years ago

2.7.5

6 years ago

2.7.4

6 years ago

2.7.3

6 years ago

2.7.2

6 years ago

2.7.1

6 years ago

2.7.0

6 years ago

2.6.0

6 years ago

2.5.0

6 years ago

2.4.14

6 years ago

2.4.13

6 years ago

2.4.12

6 years ago

2.4.11

6 years ago

2.4.10

6 years ago

2.4.9

6 years ago

2.4.8

6 years ago

2.4.7

6 years ago

2.4.6

6 years ago

2.4.5

6 years ago

2.4.4

6 years ago

2.4.3

6 years ago

2.4.2

6 years ago

2.4.1

6 years ago

2.4.0

6 years ago

2.3.12

6 years ago

2.3.11

6 years ago

2.3.10

6 years ago

2.3.9

6 years ago

2.3.7

6 years ago

2.3.6

6 years ago

2.3.5

6 years ago

2.3.4

6 years ago

2.3.3

6 years ago

2.3.2

6 years ago

2.3.1

6 years ago

2.3.0

6 years ago

2.2.5

6 years ago

2.2.4

6 years ago

2.2.3

6 years ago

2.2.2

6 years ago

2.2.1

6 years ago

2.2.0

6 years ago

2.1.0

6 years ago

2.0.10

6 years ago

2.0.9

6 years ago

2.0.8

6 years ago

2.0.7

6 years ago

2.0.6

6 years ago

2.0.5

6 years ago

2.0.4

6 years ago

2.0.3

6 years ago

2.0.2

6 years ago

2.0.1

6 years ago

2.0.0

6 years ago

1.1.1

6 years ago

1.1.0

6 years ago

1.0.6

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago