Skip to content

Instantly share code, notes, and snippets.

@blaxx
Created June 27, 2014 11:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save blaxx/a4e3e40478039375fe40 to your computer and use it in GitHub Desktop.
Save blaxx/a4e3e40478039375fe40 to your computer and use it in GitHub Desktop.
Solves the famo.us size: [true, true] headache for now
define(function(require, exports, module) {
var Surface = require('famous/core/Surface');
function ExtendedSurface(){
Surface.apply(this, arguments);
this._superDeploy = Surface.prototype.deploy;
}
ExtendedSurface.prototype = Object.create(Surface.prototype);
ExtendedSurface.prototype.constructor = ExtendedSurface;
ExtendedSurface.prototype.deploy = function deploy(target) {
this._superDeploy(target);
var size = this.getSize();
size[0] = size[0] === true ? target.offsetWidth : size[0];
size[1] = size[1] === true ? target.offsetHeight : size[1];
}
module.exports = ExtendedSurface;
});
@blaxx
Copy link
Author

blaxx commented Jun 27, 2014

@GlenTiki
Copy link

I'm using

define(function (require, exports, module) {
var Surface = require('famous/core/Surface');

function SmartSurface(options) {
    Surface.apply(this, arguments);
    this._superDeploy = Surface.prototype.deploy;
}

SmartSurface.prototype = Object.create(Surface.prototype);
SmartSurface.prototype.constructor = SmartSurface;

SmartSurface.prototype.deploy = function deploy(target) {
    this._superDeploy(target);
    var size = this.getSize();
    var width = size[0] === true ? target.offsetWidth : size[0];
    var height = size[1] === true ? target.offsetHeight : size[1];
    this.state.setSize([width, height]);
};

module.exports = SmartSurface;
});

ahaha

@kof
Copy link

kof commented Jul 31, 2014

define(function(require, exports, module) {
    'use strict'

    var Surface = require('famous/core/Surface')
    var Engine = require('famous/core/Engine')

    function AutoSizedSurface() {
        Surface.apply(this, arguments)
        this.on('deploy', this._onDeploy.bind(this))
    }

    AutoSizedSurface.prototype = Object.create(Surface.prototype)
    AutoSizedSurface.prototype.constructor = AutoSizedSurface
    module.exports = AutoSizedSurface

    AutoSizedSurface.prototype._onDeploy = function() {
        Engine.nextTick(this._detectSize.bind(this))
    }

    AutoSizedSurface.prototype._detectSize = function() {
        var size = this.getSize()
        var width = size[0] === true ? this._currTarget.offsetWidth : size[0]
        var height = size[1] === true ? this._currTarget.offsetHeight : size[1]
        this.setSize([width, height])
    }
})

@kof
Copy link

kof commented Aug 9, 2014

Found out that we don't need all this.

The timeout may vary depending on content. F.e. waiting until browser applies styles or until images within content are loaded and their sizes are detected by browser. The last one can take longer and should be avoided.

surface.on('deploy', function() {
    setTimeout(function(){
        surface.setSize(surface.getSize(true))
    }, 50)
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment