diff --git a/CHANGELOG.md b/CHANGELOG.md index 1bb8dbf95..09dec1464 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,8 @@ ### New features +- Added `start_app` method. It's a shortcut to initialize a component and mount it to the body. + ### Bug fixes ## 0.5 - Released 2019-02-01 diff --git a/examples/counter/src/lib.rs b/examples/counter/src/lib.rs index 661cb479d..58e5bcae1 100644 --- a/examples/counter/src/lib.rs +++ b/examples/counter/src/lib.rs @@ -1,9 +1,5 @@ -extern crate stdweb; -#[macro_use] -extern crate yew; - use stdweb::web::Date; -use yew::prelude::*; +use yew::{html, Component, ComponentLink, Renderable, ShouldRender}; use yew::services::ConsoleService; pub struct Model { diff --git a/examples/counter/src/main.rs b/examples/counter/src/main.rs index d900370b9..fa550b8b3 100644 --- a/examples/counter/src/main.rs +++ b/examples/counter/src/main.rs @@ -1,11 +1,3 @@ -extern crate yew; -extern crate counter; - -use yew::prelude::*; -use counter::Model; - fn main() { - yew::initialize(); - App::::new().mount_to_body(); - yew::run_loop(); + yew::start_app::(); } diff --git a/examples/crm/src/main.rs b/examples/crm/src/main.rs index 5e18678d5..5ae682192 100644 --- a/examples/crm/src/main.rs +++ b/examples/crm/src/main.rs @@ -1,11 +1,3 @@ -extern crate yew; -extern crate crm; - -use yew::prelude::*; -use crm::Model; - fn main() { - yew::initialize(); - App::::new().mount_to_body(); - yew::run_loop(); + yew::start_app::(); } diff --git a/examples/custom_components/src/main.rs b/examples/custom_components/src/main.rs index 8b7db21db..5b42a6702 100644 --- a/examples/custom_components/src/main.rs +++ b/examples/custom_components/src/main.rs @@ -1,11 +1,3 @@ -extern crate yew; -extern crate custom_components; - -use yew::prelude::*; -use custom_components::Model; - fn main() { - yew::initialize(); - App::::new().mount_to_body(); - yew::run_loop(); + yew::start_app::(); } diff --git a/examples/dashboard/src/main.rs b/examples/dashboard/src/main.rs index 3553d45ed..6a9f74453 100644 --- a/examples/dashboard/src/main.rs +++ b/examples/dashboard/src/main.rs @@ -1,11 +1,3 @@ -extern crate yew; -extern crate dashboard; - -use yew::prelude::*; -use dashboard::Model; - fn main() { - yew::initialize(); - App::::new().mount_to_body(); - yew::run_loop(); + yew::start_app::(); } diff --git a/examples/fragments/src/main.rs b/examples/fragments/src/main.rs index d81aae468..bcec50b7e 100644 --- a/examples/fragments/src/main.rs +++ b/examples/fragments/src/main.rs @@ -1,11 +1,3 @@ -extern crate yew; -extern crate fragments; - -use yew::prelude::*; -use fragments::Model; - fn main() { - yew::initialize(); - App::::new().mount_to_body(); - yew::run_loop(); + yew::start_app::(); } diff --git a/examples/game_of_life/src/main.rs b/examples/game_of_life/src/main.rs index fbffae0a9..c9832636c 100644 --- a/examples/game_of_life/src/main.rs +++ b/examples/game_of_life/src/main.rs @@ -1,9 +1,4 @@ -extern crate yew; -extern crate game_of_life; - -#[macro_use] extern crate log; -extern crate web_logger; - +use log::trace; use yew::prelude::*; use game_of_life::{Model, Msg}; @@ -11,7 +6,7 @@ fn main() { web_logger::init(); trace!("Initializing yew..."); yew::initialize(); - App::::new().mount_to_body() + App::::new().mount_to_body() .send_message(Msg::Random); yew::run_loop(); } diff --git a/examples/inner_html/src/main.rs b/examples/inner_html/src/main.rs index 4be90abf8..e5c55fd9b 100644 --- a/examples/inner_html/src/main.rs +++ b/examples/inner_html/src/main.rs @@ -1,14 +1,3 @@ -#![recursion_limit="512"] -extern crate stdweb; -extern crate yew; -extern crate inner_html; - -use yew::prelude::*; - -use inner_html::Model; - fn main() { - yew::initialize(); - App::::new().mount_to_body(); - yew::run_loop(); + yew::start_app::(); } diff --git a/examples/js_callback/src/main.rs b/examples/js_callback/src/main.rs index 27f89453d..b03f7331e 100644 --- a/examples/js_callback/src/main.rs +++ b/examples/js_callback/src/main.rs @@ -1,11 +1,3 @@ -extern crate yew; -extern crate js_callback; - -use yew::prelude::*; -use js_callback::Model; - fn main() { - yew::initialize(); - App::::new().mount_to_body(); - yew::run_loop(); + yew::start_app::(); } diff --git a/examples/large_table/src/main.rs b/examples/large_table/src/main.rs index 0e030a5eb..d055cf9b8 100644 --- a/examples/large_table/src/main.rs +++ b/examples/large_table/src/main.rs @@ -1,11 +1,3 @@ -extern crate yew; -extern crate large_table; - -use yew::prelude::*; -use large_table::Model; - fn main() { - yew::initialize(); - App::::new().mount_to_body(); - yew::run_loop(); + yew::start_app::(); } diff --git a/examples/minimal/src/main.rs b/examples/minimal/src/main.rs index c93e7354f..cb73ec584 100644 --- a/examples/minimal/src/main.rs +++ b/examples/minimal/src/main.rs @@ -1,5 +1,3 @@ fn main() { - yew::initialize(); - yew::App::::new().mount_to_body(); - yew::run_loop(); + yew::start_app::(); } diff --git a/examples/mount_point/src/main.rs b/examples/mount_point/src/main.rs index 15737aff9..c081c4d44 100644 --- a/examples/mount_point/src/main.rs +++ b/examples/mount_point/src/main.rs @@ -1,10 +1,5 @@ -#[macro_use] -extern crate stdweb; -extern crate yew; -extern crate mount_point; - -use yew::prelude::*; -use stdweb::web::{IElement, INode, IParentNode, document}; +use yew::App; +use stdweb::web::{js, IElement, INode, IParentNode, document}; use mount_point::Model; fn main() { diff --git a/examples/npm_and_rest/src/main.rs b/examples/npm_and_rest/src/main.rs index dcff62e8d..8e3af40a3 100644 --- a/examples/npm_and_rest/src/main.rs +++ b/examples/npm_and_rest/src/main.rs @@ -1,11 +1,3 @@ -extern crate yew; -extern crate npm_and_rest; - -use yew::prelude::*; -use npm_and_rest::Model; - fn main() { - yew::initialize(); - App::::new().mount_to_body(); - yew::run_loop(); + yew::start_app::(); } diff --git a/examples/routing/src/main.rs b/examples/routing/src/main.rs index 79fa597f3..4f1fec842 100644 --- a/examples/routing/src/main.rs +++ b/examples/routing/src/main.rs @@ -1,11 +1,3 @@ -extern crate yew; -extern crate routing; - -use yew::prelude::*; -use routing::Model; - fn main() { - yew::initialize(); - App::::new().mount_to_body(); - yew::run_loop(); -} \ No newline at end of file + yew::start_app::(); +} diff --git a/examples/textarea/src/main.rs b/examples/textarea/src/main.rs index 94c9f9d1d..aa00cc17e 100644 --- a/examples/textarea/src/main.rs +++ b/examples/textarea/src/main.rs @@ -1,11 +1,3 @@ -extern crate yew; -extern crate textarea; - -use yew::prelude::*; -use textarea::Model; - fn main() { - yew::initialize(); - App::::new().mount_to_body(); - yew::run_loop(); + yew::start_app::(); } diff --git a/examples/timer/src/main.rs b/examples/timer/src/main.rs index 912aa6f75..8c28e3587 100644 --- a/examples/timer/src/main.rs +++ b/examples/timer/src/main.rs @@ -1,11 +1,3 @@ -extern crate yew; -extern crate timer; - -use yew::prelude::*; -use timer::Model; - fn main() { - yew::initialize(); - App::::new().mount_to_body(); - yew::run_loop(); + yew::start_app::(); } diff --git a/examples/todomvc/src/main.rs b/examples/todomvc/src/main.rs index e1e10dd2a..005516311 100644 --- a/examples/todomvc/src/main.rs +++ b/examples/todomvc/src/main.rs @@ -1,12 +1,4 @@ -extern crate yew; -extern crate todomvc; - -use yew::prelude::*; -use todomvc::Model; - fn main() { - yew::initialize(); - App::::new().mount_to_body(); - yew::run_loop(); + yew::start_app::(); } diff --git a/examples/two_apps/src/main.rs b/examples/two_apps/src/main.rs index bfff8bfab..f2899e631 100644 --- a/examples/two_apps/src/main.rs +++ b/examples/two_apps/src/main.rs @@ -1,7 +1,3 @@ -extern crate stdweb; -extern crate yew; -extern crate two_apps; - use stdweb::web::{IParentNode, document}; use yew::prelude::*; use yew::html::Scope; diff --git a/src/lib.rs b/src/lib.rs index be431b367..4ab8d2386 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -104,56 +104,16 @@ pub fn run_loop() { stdweb::event_loop(); } -/// The Yew Prelude -/// -/// The purpose of this module is to alleviate imports of many common types: -/// -/// ``` -/// # #![allow(unused_imports)] -/// use yew::prelude::*; -/// ``` -pub mod prelude { - pub use html::{ - Component, - ComponentLink, - Href, - Html, - Renderable, - ShouldRender, - }; - - pub use app::App; - - pub use callback::Callback; - - pub use agent::{ - Bridge, - Bridged, - Threaded, - }; - - pub use events::*; - - /// Prelude module for creating worker. - pub mod worker { - pub use agent::{ - Agent, - AgentLink, - Bridge, - Bridged, - Context, - Global, - HandlerId, - Job, - Private, - Public, - Transferable, - }; - } +/// Starts an app mounted to a body of the document. +pub fn start_app() +where + COMP: Component + Renderable, +{ + initialize(); + App::::new().mount_to_body(); + run_loop(); } -pub use self::prelude::*; - /// The module that contains all events available in the framework. pub mod events { pub use html::{ @@ -203,3 +163,53 @@ pub mod events { SubmitEvent }; } + +/// The Yew Prelude +/// +/// The purpose of this module is to alleviate imports of many common types: +/// +/// ``` +/// # #![allow(unused_imports)] +/// use yew::prelude::*; +/// ``` +pub mod prelude { + pub use html::{ + Component, + ComponentLink, + Href, + Html, + Renderable, + ShouldRender, + }; + + pub use app::App; + + pub use callback::Callback; + + pub use agent::{ + Bridge, + Bridged, + Threaded, + }; + + pub use events::*; + + /// Prelude module for creating worker. + pub mod worker { + pub use agent::{ + Agent, + AgentLink, + Bridge, + Bridged, + Context, + Global, + HandlerId, + Job, + Private, + Public, + Transferable, + }; + } +} + +pub use self::prelude::*;