Utils

Framework7 utils is a set of helper methods that used internally and can be handy during development as well.

It is avaialable as a **utils** property of Framework7 class (Framework7.utils) and same property on initialized app instance (app.utils):

  1. // If we need it in place where we don't have access to app instance or before we init the app
  2. var now = Framework7.utils.now();
  3. // After we init the app we can access it as app instance property
  4. var app = new Framework7({ /*...*/ });
  5. var now = app.utils.now();

Utils Methods

parseUrlQuery

app.utils.parseUrlQuery(url)- parse url query get parameters

  • url - string - url with GET parameters. Required.

Method returns object with query parameters

  1. var query = app.utils.parseUrlQuery('http://google.com/?id=5&foo=bar');
  2. console.log(query); // { id: 5, foo: 'bar' }

serializeObject

app.utils.serializeObject(object)- Create a serialized representation of a plain object suitable for use in a URL query string

  • object - object - Object to serialize

returns a new unique array

  1. var params = { foo: 'bar', id: 5 };
  2. console.log(app.utils.serializeObject(params)); // 'foo=bar&id=5'

requestAnimationFrame

app.utils.requestAnimationFrame(callback)- Cross-browser implementation on requestAnimationFrame

  • callback - function - function to call when it’s time to update your animation for the next repaint

returns animation request id, that uniquely identifies the entry in the callback list

  1. var animId;
  2. function anim() {
  3. var left = parseInt($$('#anim').css('left'), 10) + 1;
  4. $$('#anim').css({left: left + 'px'})
  5. animId = app.utils.requestAnimationFrame(anim);
  6. }
  7. animId = app.utils.requestAnimationFrame(anim);

cancelAnimationFrame

app.utils.cancelAnimationFrame(requestID)- Cancels an animation frame request

  • requestID - number - The ID value returned by the call to app.utils.requestAnimationFrame() that requested the callback
  1. app.utils.cancelAnimationFrame(animId);

removeDiacritics

app.utils.removeDiacritics(text)- Replace diacritics in specified text string with standard latin characters

  • text - string - Text string
  1. var text = app.utils.removeDiacritics('ÁÓL');
  2. console.log(text); //-> 'AOL'

nextFrame

app.utils.nextFrame(callback)- Executes code on next available animation frame.

  • callback - string - function to call when it’s time to update your animation for the next repaint.
  1. app.utils.nextFrame(function() {
  2. // do something on next frame
  3. });

nextTick

app.utils.nextTick(callback, delay)- executes code after required delay. Basically alias for setTimeout

  • callback - string - function to call after specified delay
  • delay - number - delay in ms. Optional, by deault is 0

returns timeout ID

  1. app.utils.nextTick(function() {
  2. // do something on next tick
  3. });

now

app.utils.now()- returns current timestamp in ms

  1. var now = app.utils.now();
  2. setTimeout(function () {
  3. var timeDiff = app.utils.now() - now;
  4. console.log(timeDiff + 'ms past');
  5. }, 2000);

extend

app.utils.extend(target, …from)- extends target object with properties and methods from from objects

  • target - object - target object to extend
  • from - object - objects to copy properties and methods from

returns target object with extended properties and methods

This method becomes very handy if you need to extend one object with properties of others or when you need a deep copy of an object.

  1. var a = {
  2. apple: 0,
  3. cherry: 97
  4. };
  5. // Pass as empty object as target to copy a into b
  6. var b = app.utils.extend({}, a);
  7. console.log(b); // { apple: 0, cherry: 97 }
  8. console.log(a === b); // false
  1. var a = {
  2. apple: 0,
  3. cherry: 97
  4. };
  5. var b = {
  6. banana: 10,
  7. pineapple: 20,
  8. }
  9. // Extends a with b
  10. app.utils.extend(a, b);
  11. console.log(a); // { apple: 0, cherry: 97, banana: 10, pineapple: 20 }
  1. var a = {
  2. apple: 0,
  3. cherry: 97
  4. };
  5. var b = {
  6. banana: 10,
  7. pineapple: 20,
  8. }
  9. // Create new object c from the merge of a and b
  10. var c = app.utils.extend({}, a, b);
  11. console.log(c); // { apple: 0, cherry: 97, banana: 10, pineapple: 20 }
  1. var a = {
  2. apple: 0,
  3. cherry: 97
  4. };
  5. var b = {
  6. apple: 10,
  7. pineapple: 20,
  8. }
  9. // Extend a with b
  10. app.utils.extend(a, b);
  11. console.log(a); // { apple: 10, cherry: 97, pineapple: 20 }

uniqueNumber

app.utils.uniqueNumber()- returns unique number, increased by 1 with every call

  1. app.utils.uniqueNumber(); // -> 2
  2. app.utils.uniqueNumber(); // -> 3
  3. app.utils.uniqueNumber(); // -> 4

id

app.utils.id(mask, map)- generates random ID-like string

  • mask - string - ID string mask, by default is xxxxxxxxxx
  • map - string - characters that will be used for generation, by default is 0123456789abcdef

returns randomly generated string

  1. app.utils.id() // -> ffe28ab56e
  2. app.utils.id('xxxx-xxxx-xxxx-xxxx') // -> 1ea3-f127-dc67-627d
  3. app.utils.id('xxxx-xxxx', 'abcd') // -> aabc-ccda