lundi 14 décembre 2015

AngularJS load data (settings) from server

I've made a simple food-ordering application with Laravel 5. Now I'm trying seperate the front-end and back-end by using AngularJS. Angular grabs it's data from the back-end, which is a REST API based on Laravel (Lumen).

I have a few settings stored in the database which consist of a key-value pair. Think of settings like 'minimum-order-amount' and 'delivery-costs'. Normally I would simply call setting(key) within my Laravel application:

/**
 * Get the value for the given setting from the database.
 *
 * @param  mixed  $key
 * @return mixed
 */
function setting($key)
{
    static $settings;

    if(is_null($settings)) {
        $settings = Cache::rememberForever('settings', function() {
            return array_pluck(App\Setting::all()->toArray(), 'value', 'key');
        });
    }

    return (is_array($key)) ? array_only($settings, $key) : $settings[$key];
}

Notice the static functions which allows me to call the setting() function multiple times per request, but only query the database once. How can I achieve this within my Angular application?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire