Fork me on GitHub
This API is deprecated since version 1.3.0 and no longer available since version 1.3.1

AngularMeteorObject

An object that connects a Meteor Object to an AngularJS scope variable


This module has been deprecated in favor of the new helpers API.

There is no need for $meteor.object anymore as we can use Mongo Collection’s findOne function. Helpers will make sure to update Angular. We also removed autobind because it's a bad practice and we gain much better performance and easier maintainability both for the library and the apps developed with it.

Here is an example for how to migrate:

Old code:

$scope.party = $meteor.object(Parties, $stateParams.partyId);

New Code:

$scope.helpers({
  party() {
    return Parties.findOne($stateParams.partyId);
  }
});

Usage

See $meteor.object


Methods

save( :doc );

Parameters

Param Type Details Required
doc empty/ object

Document properties to save to the meteor object data.

No

If a document is passed, the method will compare between the document and the data, and will save the necessary changes into AngularMeteorObject. If no document argument is passed, the method saves all current data on the AngularMeteorObject, even unchanged properties will be overridden with their current values, hence hooks may be triggered on them. Returns a promise with value of 1 when successful (this is the number of docs successfully updated) or an error.

reset();

Reset the current value of the object to the one in the server.

subscribe( :subscriptionName );

A shorten (Syntactic sugar) function for the $meteor.subscribe function.

The function takes only one parameter and does not return a promise like $meteor.subscribe does.

When called after $scope.meteorObject, it acts the same but in addition it automatically closes the subscription when the scope is destroyed.

Parameters

Param Type Details Required
subscriptionName subscriptionName

The subscription name to subscribe to. exactly like the first parameter in $meteor.subscribe service.

Yes

getRawObject()

Parameters

None

Returns a copy of the AngularMeteorObject with all the AngularMeteor-specific internal properties removed. The returned object is then safe to use as a parameter for method calls, or anywhere else where the data needs to be converted to JSON.

stop()

Parameters

None

Stops watching the object for changes.


Example

// Define a new Meteor Mongo Collection
Parties = new Mongo.Collection('parties');

// Define the Schema using aldeed:collection2 package for the Parties
PartySchema = new SimpleSchema({
  name: {
    type: String
  },
  description: {
    type: String
  },
  nameModified: {
    type: Date,
    autoValue: function() {
      if (this.field("name").isSet) {
        return new Date(); // automatically set nameModified with new Date only when name is modified
      }
    },
    denyInsert: true,
    optional: true
  }
});

Parties.attachSchema(PartySchema);

Meteor.methods({
  doFancyStuff: function(party) {
    var id = party._id;
    // Do something fancy, like link with partygoers.

    party.description = "Set to fancy description!";

    delete party._id;

    Parties.update({_id: id}, {$set: { party }});
  }
});

if (Meteor.isClient) {

  angular.module("socially").controller("PartyDetailsCtrl", ['$scope', '$stateParams', '$meteor',
    function($scope, $stateParams, $meteor){

      $scope.party = $meteor.object(Parties, $stateParams.partyId);

      $scope.partyNotAuto = $meteor.object(Parties, $stateParams.partyId, false);

      $scope.save = function(docs) {
        $scope.partyNotAuto.save(docs).then(function(numberOfDocs){
          console.log('save success doc affected ', numberOfDocs);
        }, function(error){
          console.log('save error', error);
        });
      };

      // all values will be rewritten with their current values
      // nameModified will change
      $scope.save()

      // only description will change
      $scope.save({description: 'wine and cheese'})

      $scope.reset = function() {
        $scope.partyNotAuto.reset();
      };

      // Call a method that does fancy stuff to the object.
      $scope.callMethod = function() {
        $meteor.call('doFancyStuff', $scope.party.getRawObject());
      };
  }]);
}