Enquire.js and Angular

587 views
Skip to first unread message

Paul Hammant

unread,
Feb 4, 2013, 11:24:39 PM2/4/13
to ang...@googlegroups.com
Enquire.js = http://wicky.nillia.ms/enquire.js

Has anyone played with this and Angular together?  For no particular reason other than a challenge, I'm trying to see if I can get Enquire.js and Angular to play together. The goal is to avoid media queries, and yet still be responsive to form-factors.

Enquire.js is going to invoke JavaScript functions if, say, a screen width is 768px or below, as well as one for higher than that.  That's outside of Angular's controller scope though.  Any ideas as to how to get them to work together?

- Paul

Florian Orben

unread,
Feb 5, 2013, 7:39:13 AM2/5/13
to ang...@googlegroups.com
Hey,

that shouldn't be too hard I guess:
Attach a namespace object to the window object: var myNamepace = {}; //outside of angular's scope

Then i'd create a service responsible for handling the enquire.js logic so it could be shared among different controllers/directives...
myApp.factory('EnquireService', ['$window', function($window) {
  screenWidth1000Match = function() { /* doSomething */ }; //or some other name, you get the point

  //attach to previously created object
  $window.myNamespace.screenWidth1000Match = screenWidth1000Match;
}])

//outside of angular's scope again register the function with enquire:
enquire.register("screen and (max-width:1000px)", {
  match : myNamespace.screenWidth1000Match
});

- Flo

Florian Orben

unread,
Feb 5, 2013, 7:46:40 AM2/5/13
to ang...@googlegroups.com
Nvm what I just posted, you could probably do it even easier since enquire is available as a global

myApp.factory('EnquireService', ['$window', function($window) {

  var myWidth = 50; //or sth like that
  enquire.register("screen and (max-width:1000px)", {
    match: function () {
      myWidth = 200;       
    }
  }).register("screen and (max-width:320px)", {
    match: function() {
      myWidth = 20;
    }
  });

  return {
    width: myWidth
  };

}]);
Reply all
Reply to author
Forward
0 new messages