A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.npmjs.com/package/atvjs below:

atvjs - npm

atvjs

Blazing fast Apple TV application development using pure JavaScript.

Philosophy What?

This is a super simple framework for blazing fast Apple TV application development using pure JavaScript. It relies on the tvOS provided TVML and TVJS for Apple TV development. However this framework does most of the heavy lifting for you and lets you concentrate on your application logic without worrying about the hassles of complicated architecture for Apple TV development. Build your Apple TV application the same way how you are used to building your SPA applications in JavaScript and let the framework handle the rest for you.

Why?

The existing application architecture and sample code provided by apple for building the Apple TV applications using TVML and TVJS appears to be very immature (no offense Apple) and feels more like the web applications of the 90s where each of the page loads separately by sending the request to the back-end which generates your application page markup based on the user interaction and sends the new page back to the browser. This is okay if we were still in the 90s but feels so dumb in this era where we are used to building SPAs with REST APIs and back-end is used just as a data interaction point. Here comes atvjs that bridges the gap and lets you use the same architecture that, we as front-end developers, have embraced over the years.

How?

You simply create your pages (views) by passing a bunch of configurations (name of the page, template function, url etc) with your desired data (you can either populate data manually using your own ajax requests or pass a url configuration to fetch data from your server). Once the page is created, it is uniquely identifiable using the name. You can navigate to your page anytime in your application using the same name.

What's included Getting Started

atvjs is defined as a UMD and available as an npm package. You can either import it as a dependency or download it independently and include in your project.

$ npm install --save atvjs

You'll then be able to use it as a dependency with your favorite module system.

import ATV from 'atvjs';

var ATV = require('atvjs');

Or if you have downloaded a copy of atvjs, pull the script inside your application after launch.

function onEvaluate(success) {

    if (!success) {

        

    }

}

 

App.onLaunch = function(options) {

    var baseurl = options.BASEURL;

    App.launchOptions = options;

    evaluateScripts([`${baseurl}atv.min.js`, `${baseurl}app.js`], onEvaluate);

};

 

Basic Examples Creating Pages

Create pages in your application using the page factory. You will then be able to navigate to these pages using the name of the page.

ATV.Page.create({

    name: 'home',

    

    

    template(data) {

        return `<document>

                    <alertTemplate>

                        <title>${data.title}</title>

                        <description>${data.description}</description>

                    </alertTemplate>

                </document>`;

    },

    

    

    data: {

        title: 'Homepage',

        description: 'This is my super awesome homepage created using atvjs.'

    }

});

 

ATV.Navigation.navigate('home');

Adding TVML styles to your page

You need to define your TVML styles as string and pass that as a configuration to your page. It will automatically be added to your page document in the runtime when your page is being navigated.

let myPageStyles = `

.text-bold {

    font-weight: bold;

}

.text-white {

    color: rgb(255, 255, 255);

}

`;

 

ATV.Page.create({

    name: 'home',

    style: myPageStyles,

    template: your_template_function,

    data: your_data

});

Fetching data from a remote source

You can fetch JSON content from the remote api by setting the url configuration. The data will be fetched using ajax and will be applied to the provided template. You can even run some transformations on the data before applying it to the template

ATV.Page.create({

    name: 'home',

    url: 'path/to/your/api/that/returns/json',

    template: your_template_function

});

 

ATV.Page.create({

    name: 'home',

    url: 'path/to/your/api/that/returns/json',

    template: your_template_function,

    data(response) {

        

        let transformedData = someTransformationOfResponse(response);

        return transformedData;

    }

});

Creating links to other pages

You can setup links to other pages directly in your TVML markup by setting the data-href-page attribute with the value of your page name that you want to link to. The framework will take care of the navigation for you. You can also pass options to your page (see topic Custom options while navigation for details) by setting up the data-href-page-options attribute with a JSON value.

<document>
    <alertTemplate>
        <title>Example for creating links to other pages</title>
        <description>Select an option</description>
        <button data-href-page="homepage">
            <text>Go To Homepage</text>
        </button>
        <button data-href-page="login" data-href-page-options='{"username": "emadalam", "password": "123456"}'>
            <text>Login</text>
        </button>
    </alertTemplate>
</document>
Advanced Topics Event Handling

You can define the list of events and their respective handlers as key-value pairs. The handler will be invoked in the current object context and you can access all the methods and properties that exist on the object.

ATV.Page.create({

    name: 'mypage',

    template: your_template_function,

    data: your_data,

    events: {

        select: 'onSelect',

        highlight: 'onHighlight'

    },

    

    

    

    onSelect(e) {

        let element = e.target;

        let someCheckForElementType = element.getAttribute('data-my-attribute');

        let someOtherCheckForElementType = element.getAttribute('data-my-other-attribute');

 

        if (someCheckForElementType) {

            this.doSomethingOnElementType1();

        }

 

        if (someOtherCheckForElementType) {

            this.doSomethingOnElementType2();

        }

    },

    onHighlight(e) {

        

    },

    doSomethingOnElementType1() {

        

    },

    doSomethingOnElementType2() {

        

    }

});

Custom options while navigation

You can pass your application state/logic to make reusable dynamic pages that renders a new page each time the navigation is performed. This can be achieved by setting a ready method in the configuration that accepts 3 parameters, options, resolve and reject. These parameters are automatically passed to the ready method whenever a navigation to this page is performed. The navigation relies on JavaScript Promises, so you'll have to call resolve/reject method after performing your logic.

ATV.Page.create({

    name: 'login',

    template: your_template_function,

    

    

    

    

    

    ready(options, resolve, reject) {

        let data = {

            username: options.username,

            password: options.password

        };

        

        

        ATV

            .Ajax

            .post('someURL', {data: data})

            .then((xhr) => {

                

                let response = xhr.response;

                

                

                resolve({

                    name: response.name,

                    message: response.message

                });

            }, (xhr) => {

                

                let response = xhr.response;

                reject({

                    status: xhr.status,

                    message: response.message

                });

            });

    }

});

ATV.Navigation.navigate('login', {username: 'emadalam', password: '123456'});

Creating Menu Page

The way menu template is designed in TVJS, you need to first create a menu template with your list of menu items. You then need to create individual documents and set one for each of the menu item. The resultant menu template then needs to be parsed and converted into a document which you can push on the navigation stack. Sounds fancy? NO WAYS! Here comes atvjs for your rescue. All you need is a menu configuration with your items list and the pages that you want to associate. The rest will be taken care by the framework. You can then navigate to the menu page using the provided navigation method. SWEET!

Note: Menu page is singleton, meaning you cannot create multiple menu pages. It seems logical, as an application at any given state, will have a single menu listing in its entire lifespan.

let SearchPage = ATV.Page.create({});

let HomePage = ATV.Page.create({});

let MoviesPage = ATV.Page.create({});

let TVShowsPage = ATV.Page.create({});

 

ATV.Menu.create({

    

    attributes: {},

    

    rootTemplateAttributes {},

    

    items: [{

        id: 'search',

        name: 'Search',

        page: SearchPage

    }, {

        id: 'homepage',

        name: 'Home',

        page: HomePage,

        attributes: {

            autoHighlight: true, 

            reloadOnSelect: true 

        }

    }, {

        id: 'movies',

        name: 'Movies',

        page: MoviesPage

    }, {

        id: 'tvshows',

        name: 'TV Shows',

        page: TVShowsPage

    }]

});

 

ATV.Navigation.navigateToMenuPage();

Application initialization using configuration

You can easily initialize your application by passing all the configurations at once.

let SearchPage = ATV.Page.create({});

let HomePage = ATV.Page.create({});

let MoviesPage = ATV.Page.create({});

let TVShowsPage = ATV.Page.create({});

let LoginPage = ATV.Page.create({});

 

const loaderTpl = (data) => `<document>

    <loadingTemplate>

        <activityIndicator>

            <title>${data.message}</title>

        </activityIndicator>

    </loadingTemplate>

</document>`;

 

const errorTpl = (data) => `<document>

    <descriptiveAlertTemplate>

        <title>${data.title}</title>

        <description>${data.message}</description>

    </descriptiveAlertTemplate>

</document>`;

 

let globalStyles = `

.text-bold {

    font-weight: bold;

}

.text-white {

    color: rgb(255, 255, 255);

}

.dark-background-color {

    background-color: #091a2a;

}

.button {

    background-color: rgba(0, 0, 0, 0.1);

    tv-tint-color: rgba(0, 0, 0, 0.1);

}

`;

 

ATV.start({

    style: globalStyles,

    menu: {

        attributes: {},

        items: [{

            id: 'search',

            name: 'Search',

            page: SearchPage

        }, {

            id: 'homepage',

            name: 'Home',

            page: HomePage,

            attributes: {

                autoHighlight: true 

            }

        }, {

            id: 'movies',

            name: 'Movies',

            page: MoviesPage

        }, {

            id: 'tvshows',

            name: 'TV Shows',

            page: TVShowsPage

        }]

    },

    templates: {

        

        loader: loaderTpl,

        

        error: errorTpl,

        

        status: {

            '404': () => errorTpl({

                title: '404',

                message: 'The given page was not found'

            }),

            '500': () => errorTpl({

                title: '500',

                message: 'An unknown error occurred, please try again later!'

            })

        }

    },

    

    handlers: {

        select: {

            globalSelecthandler(e) {

                let element = e.target;

                let someElementTypeCheck = element.getAttribute('data-my-attribute');

 

                if (elementTypeCheck) {

                    

                }

            }

        }

    },

    onLaunch(options) {

        

        ATV.Navigation.navigateToMenuPage();

        

        

    }

});

Sample Code Useful Links Contributions License

atvjs is released under the MIT License.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4